Automating Shell Scripts and Python Programs Using CRON

There are a couple of reasons you may want to configure the Pi to start programs automatically. And honestly, a lot of projects need you to do so, especially in the embedded sector. So in this tutorial, we will make a program start on boot or run in regular time intervals on a Raspberry Pi.

CRON

Figure 1: Cron logo

Cron is a time-based task scheduler tool in Linux-based operating systems like the Raspberry Pi OS. You can use cron to run programs on boot or run programs periodically at fixed intervals, which is particularly helpful with recurring tasks. Backing up data after the cutoff, sending an office-wide greeting every morning, and sending sensor data every hour are just some of its applications.

There are various ways to automate a program in Raspberry Pi OS, but using cron is the easiest. Firstly, the tasks are not limited to shell scripts. You can also automate python programs, video players, and much more software. Second, the time intervals are configurable for up to a specific minute. Not all methods can do both. If you want to know more about the other ways, I can write about them in the next feature. For now, let’s focus on cron.

RUNNING A PROGRAM ON STARTUP

1. First. open your crontab using the command below.

sudo crontab -e

Crontab is a command that gives you access to the current user’s cron table. It simply lists the scheduled tasks. Further, all users on your device have it. Even the root user has its own cron table.

2. Next, choose a text editor. In my experience, I was asked for my preferred text editor when I used the command line using the ALT + F1 shortcut. I tried it again using the desktop terminal and it didn’t ask me. It just opened the file using nano. Using a different text editor won’t affect anything so choose the one you’re comfortable with.

See also  Arduino vs. Raspberry Pi: Choosing the Right Platform for Your Project
Figure 2: Crontab on GNU Nano

3. Next, add the program you want to run on boot at the end of the file.

@reboot python3 /home/pi/filename.py

In order to run the program on boot, the line must have @reboot at the beginning. After that you can add whatever. For our example, we run the python3 program to launch the python3 interpreter followed by the path to the python script.

4. Lastly, save and exit.

Running a program on startup requires you to have execute permission with the application and file. You can check using ls -l. Otherwise, to add the permission, enter sudo chmod a+x FILENAME.

RUNNING A PROGRAM AT REGULAR INTERVALS

1. Open crontab.

 sudo crontab -e

2. Choose a text editor.

3. Add a scheduled task.

In order to that, we must get familiarized with the format of a cron table entry. Notice the last line:

# m h  dom mon dow   command

The layout for a cron entry is made up of six components: minute, hour, day of month, month of year, day of week, and the command to be executed.

# * * * * *  command to execute
# ? ? ? ? ?
# ? ? ? ? ?
# ? ? ? ? ?
# ? ? ? ? ?????? day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# ? ? ? ??????????? month (1 - 12)
# ? ? ???????????????? day of month (1 - 31)
# ? ????????????????????? hour (0 - 23)
# ?????????????????????????? min (0 - 59)

For instance, suppose you want to run a program named filename.py every 3 o’clock in the afternoon. The entry should look like this:

15 0 * * *  python3 /home/pi/filename.py

4. Save and exit.

5. To view your currently scheduled tasks, enter the command below:

crontab -l

For more tutorials on Raspberry Pi, check File Permissions on Linux and Exploring Linux Root!