I´ve explained the basics of Scripting with Python in my former article, but what if you want to run a script periodically on your Raspberry Pi? Well, there is this crazy little thing called “Cron Job”. It is quite easy to set them up on your Raspberry, this is how.
Basic Cron Jobs
cd /etc/cron.d ls
This is where all the magic happens. The folder will be empty, which means that there is no Cron Job (yet). Earlier you would have used “crontab”, but afaik it´s outdated and the cron.d folder is what the cool kids are using nowadays. Basically, you create a file for each Cron Job, you can NOT use folders to organize them, but you can put more than one Cron Job in one file. So let´s create our first Cron Job. First, we create a shell script for testing in our home folder and make it executable (don´t worry, i will cover Python scripts later):
mkdir /home/pi/scripts nano /home/pi/scripts/cron.sh
Btw, you can also use the vi editor, but i prefer nano. vi is only for super-nerds.
Now let´s just put something in for testing, the following code puts the actual time in a file whenever the Cron Job runs:
#!/bin/sh date >>/home/pi/cron.txt
Save the file, close nano (CTRL+X) and use chmod to make it executable:
chmod +x /home/pi/scripts/cron.sh
Of course you can also just switch to the home folder for all that stuff to avoid writing /home/pi the whole time, but we´re back in /etc/cron.d now:
sudo nano mycronjob
Now enter the following line in the editor:
* * * * * pi /home/pi/scripts/cron.sh
A small but important side note: ALWAYS add a line break to your Cron Job. If your Cron Job does not run it´s probably the reason why.
Alright, so what does that line mean? The five stars define the minute, hour, day, month and day of the week (in that order). After the stars, we add the user context for the Cron Job so we can write into the pi folder. The last parameter is the script that should get run, obviously. A simple star at the first spot means “run the cron job every minute”, a star at the second spot means “run the cron job every hour” and so on.
That means, our Cron Job runs every minute of every day. I will not get into this in detail, but if you want to run your Cron Job every 10 minutes, you need to divide by 10:
*/10 * * * * pi /home/pi/scripts/cron.sh
…or if you want to run your script every minute on sundays and tuesdays, this is what you need:
* * * * 0,2 pi /home/pi/scripts/cron.sh
…or if you want to run your script every 30 minutes between 09:00 and 17:00:
*/30 9-17 * * * pi /home/pi/scripts/cron.sh
After saving and closing the Cron Job file, there is nothing else to do. Your Cron Job is set up and should hopefully work, just check the text file (cron.txt) after a minute!
Cron Jobs with Python
This is as easy as using a shell script. Make sure the Python script is executable (chmod and stuff), this would be a good script for testing:
f = open('/home/pi/cron.txt','a') f.write('test\n') f.close()
Now it´s time to add the Cron Job for our script, either create a new file or use the existing “mycronjob” in /etc/cron.d:
* * * * * pi /usr/bin/python3 /home/pi/scripts/test.py
If everything is correct, you should see a new “test” entry in the text file every minute.
One more thing about Cron Jobs: It is not possible to use seconds to run them. The minimum amount of time between two runs is one minute. If you need to repeat something faster, Cron Jobs may not be the way to go.