Skip to content

Setting Up Automation on macOS

RileyXX edited this page Jan 9, 2024 · 4 revisions

Creating a shell script to run multiple Python projects or programs at once

This guide will walk you through the steps to create a shell script that can run multiple Python projects or programs. Additionally, it includes optional steps for opening programs before execution.

Steps

  1. Open a text editor and create a new file called run_projects.sh.

  2. Add the following lines to the file, replacing project1, project2, and project3 with the actual names of your Python projects or programs. You can add as many lines as needed for each project or program.

    #!/bin/bash
    
    python -m project1
    
    python -m project2
    
    python -m project3
    

    If you want to open programs before running the Python projects, add the following lines to the script, replacing program1 and program2 with the actual commands to open the programs. You can add as many lines as needed for each program.

    open -a program1
    
    open -a program2
  3. Save the file, for example, run_projects.sh.

Creating a Shortcut with an Icon for Scripts or Programs

This section explains how to create a shortcut with an icon for easy on-demand execution of scripts or programs on macOS by creating an alias.

Steps

  1. Locate the script or program you want to create a shortcut for.

  2. Right-click on the script or program and select "Make Alias". This will create an alias of the script or program.

  3. Rename the created alias with an appropriate name, for example, Run Projects.

  4. Drag the alias to your desktop or any desired location.

  5. Right-click on the alias and select "Get Info". This will open the "Get Info" window for the alias.

  6. In the "Get Info" window, click on the icon in the top-left corner to select it.

  7. Press "Cmd + C" to copy the icon.

  8. Close the "Get Info" window.

Now you have a shortcut with the assigned icon for your script or program. Double-click the shortcut to execute the script or program, running your Python projects or programs (if included). The creation of a shortcut with an icon provides a convenient way to execute scripts or programs on demand.

  1. Run the Script or Program:

    • Double-click the shortcut to execute the script or program. This will run the specified Python projects or programs (if included) in the order specified.

That's it! By following these instructions, you have successfully created a shell script to run multiple Python projects or programs at once, with the option to open programs before execution, and created a shortcut with an icon (alias) for easy on-demand execution from the desktop.

Auto-running a file on login, once per day, or hourly using cron

This guide will walk you through the steps to configure cron to automatically run any file on login, once per day, or hourly.

Steps

  1. Open a terminal.

  2. Edit the cron table by running the following command:

    crontab -e
  3. If prompted, choose your preferred text editor.

  4. Configure the cron job:

    • To run a file on login:

      • Add the following line to the cron table:

        @reboot /path/to/your/file.sh

        Replace /path/to/your/file.sh with the actual path to the file you want to run on login.

    • To run a file once per day:

      • Add the following line to the cron table:

        0 0 * * * /path/to/your/file.sh

        Replace /path/to/your/file.sh with the actual path to the file you want to run once per day.

    • To run a file hourly:

      • Add the following line to the cron table:

        0 * * * * /path/to/your/file.sh

        Replace /path/to/your/file.sh with the actual path to the file you want to run hourly.

  5. Save the cron table and exit the text editor.

  6. The cron job is now scheduled to run the specified file at the specified time interval.

That's it! You have successfully set up cron to auto-run a file on Linux.