Skip to content

Setting Up Automation on Linux

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.

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

Creating a Shortcut with an Icon

This section explains how to create a launcher shortcut with an icon for easy on-demand execution of the shell script.

Steps

  1. Right-click on the desktop and select "Create Launcher" or "Create Shortcut".

  2. In the launcher creation dialog, fill in the following information:

    • Name: "Run Projects"
    • Command: Browse and select the location of the run_projects.sh script you created.
    • Icon: Browse and select an icon file of your choice.
  3. Click "OK" or "Create" to save the launcher shortcut.

Now you have a shortcut on your desktop that you can double-click to execute the shell script and run your Python projects or programs (if included). The creation of a shortcut with an icon provides a convenient way to execute the script on demand.

  1. Run the Shell Script:

    • Double-click the run_projects.sh script or the launcher shortcut you created to execute it. 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 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.