Cron is the name of program that enables Raspberry Pi users to execute commands or scripts (groups of commands) automatically at a specified time/date. Although its reasonably straightforward to set up, if it doesn't work then it can be tricky to work out where the underlying problem is. This troubleshooting guide to cron on the Raspberry Pi should help with the debugging process.
1. Check that cron is running
Firstly, check that cron is running:
ps -eaf | grep cron
This should return something like this:
root 2136 1 0 Dec02 ? 00:00:01 /usr/sbin/cron
If you don't get a similar result from the above ps command then cron may not be working. You can restart it by typing:
sudo /etc/init.d/cron restart
If it wasn't running, you may want to do some more investigation using:
2. Check your paths are set correctly
If you're running something like:
12 04 * * * python myscript.py
in your crontab then it may not work because the absolute path needs to be used for both your program and script, so something like the following would work:
12 04 * * * /usr/bin/python2.7 /home/pi/Projects/myscript.py
3. Check permissions
Check that the permissions of the program, the script, the directories and relevant subdirectories have the correct permissions to allow the crontab to run the program. If you're running the crontab as root (i.e. sudo crontab -e) you shouldn't have to modify permissions but if you're using crontab -e then you may.
4. Check environmental variables
Next, test the commands that you've put into the crontab are using the same environment that crontab uses.To do this enter:
env -i /bin/bash --noprofile --norc
Now run the commands from the command line and see if they work. e.g.:
If they do, then your cron environmental variables are set up correctly.
5. Check the crontab is set up correctly
This is a pretty simple error, but always worthwhile checking. The format of your crontab should be like this:
.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * user-name command to be executed
6. Send the output of your crontab to either a mail or file
To send the crontab output to an email, you'll need to have the MAIL variable set up at the top of the crontab, as follows:
You'll also need to have your email service configured correctly too.
To send the output of your crontab command to a file, set it up as follows:
23 29 * * * /home/pi/backup.sh > /home/pi/logs/backup.log 2>&1
In the above,
indicates that the standard output of the backup.sh script will be redirected to the backup.log file. 2>&1 indicates that the standard error (2>) is redirected to the same file descriptor that is pointed by standard output (&1). So, both standard output and error will be redirected to
7. Set it to run every minute
Setting your command to run every minute makes it easier to debug, instead of having to wait until a specified time to test it which can become tedious. This is done by:
* * * * * /home/pi/backup.sh > /home/pi/logs/backup.log 2>&1
As you'll know if you've searched for this article, troubleshooting cron jobs is not an easy process. Following the above steps should make the finding a crontab error much easier.