Raspberry Pi for N00bs – Cron Jobs

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.

15 thoughts on “Raspberry Pi for N00bs – Cron Jobs”

  1. Very clearly explained.. excellent. Thanks for the unassuming approach, those who know can skip over or re-learn.

  2. Thanks this is really helpful, one thing that I learned is that don’t trust GUI on raspberry pi for these things

  3. How to execute a python script at boot time itself(my script file has coding to play a audio and need to play before we will type the login id && password)..I tried from many sites u had the best and I did the same thing u did just by placing @reboot at the begin of the line as below,,,

    @reboot pi /usr/bin/python3 /home/pi/scripts/test.py
    but not working..
    Thanks in advance!

          1. please help me to run a simple python script at start up? (for eg. let the script print hello) please explain me step by step,,,starting from basic steps!

            I tried many examples using init.d & rc.local but not worked… may be I had gone wrong! so be precise,clear,step by step starting from basic step!

  4. Thanks, this helped me to realize that crontab -e seems to be dead now as it no longer works on Raspberry Pi 2 but using /etc/cron.d works. Thanks so much!

  5. Hi there do you know where i can see the time my cron uses? Cause i dont seem to have any luck with the timing. Only the * * * * * pi scriptlocation.sh
    If i try to do 0 15 * * * pi scriptlocation.sh i just does not execute at 15:00 my time is CEST if i try 0 13 * * * thats my UTC time it doesnt work either.

    1. try it step by step, for example with only changing the minute, to see what exactly does not work correctly. “0 * * * *” should run once every hour. try that first. but in general, it looks good with “0 15 * * *”, to be honest…

  6. I had no idea that
    crantab -e
    and
    cron.d
    were different.

    I have bee playing around with crontab -e and I am having no luck… I am going to try your cron.d instead. Thank you.

  7. cron lines have the format ‘m h dom mon dow command’, so why do you preface the executable command with ‘pi’ in ‘*/10 * * * * pi /home/pi/scripts/cron.sh’? shouldn’t ‘*/10 * * * * /home/pi/scripts/cron.sh’ be sufficient?

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.