Define your PATH
When running stuff from an interactive shell under your regular account you’re probably using the PATH variable. Cron runs non-interactively and probably not from your shell so it might not know where to find your script or program. To fix this, define PATH in cron:
From your account do
$ echo $PATH
Output will be something like
In your crontab enter:
Cron must end in newline
I don’t know if this one is Debian specific but if you’ve ever wondered why the last job in your crontab won’t run you might add a newline to it. Just open your crontab and add an extra (empty) line underneath the last job.
Have cron log to syslog
If you’re running rsyslogd you can have cron log to syslog by opening /etc/rsyslog.conf and uncommenting the line that reads
# cron.* /var/log/cron.log
Then reload rsyslogd:
service rsyslogd restart
To watch for cronjobs:
# tail -f /var/log/syslog | grep -i cron
Set your default shell
This might make you lazy but instead of defining your shell in every script you can define it in your crontab. Easy for commands or small scripts you’re running from cron itself. In your crontab write:
…or whatever your shell is.
Run a script every odd minute
This is a fun one. Running a script every two minutes is easy:
*/2 * * * * /path/to/script.sh
This will run your script at minute 2, 4, 6, etc. of every hour. But what if you want your script to run at minut 1, 3, 5, and so on? Well, the first * in */2 is the range. You could define your range as 0-59, which is the same as any minute, so *. But you don’t have to. You could say 20-40 and your script would run at the same interval (namely the one defined after the slash: 2) from the 20th until the 40th minute. To set an ofset of one minute, do:
1-59/2 * * * * /path/to/script.sh
This will run your script every two minutes, starting from minute number one, resulting in your script running every odd minute.
Mute cron’s output
To stop cron from sending out mail with any generated output add to your job:
It will look something like this:
* * * * * /path/to/script.sh>/dev/null 2>&1
This will redirect errors (2) and (&) regular output (1) to /dev/null, which is to say: to Never Never Land. Be advised that you will not be warned of any errors anymore so be sure to build that into your script.
Have cron mail to a different address than root
Don’t bother with alias files. Just add to your crontab:
Substitute with your own address, of course.
There are more things about cron but these are the things that I wrote down over time because I found them very useful. The most usefull thing, as ever, is to comment a lot. It’s better two write too much comment than not enough.
Comment your code like you need to explain it to a monkey. Your future self will thank you for it.
Your crontab may look like this:
# m h dom mon dow command SHELL=/bin/bash PATH=/home/vorkbaard/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games MAILTOemail@example.com # Run a job every five minutes */5 * * * * /path/to/script.sh # Run a job daily @daily /path/to/script.sh # Every odd minute and don't send ANY output 1-59/2 * * * * /path/to/script.sh > /dev/null/ 2>&1 # Run two times a day 30 10,16 * * * /path/to/script.sh # Workin' nine to five * 9-17 * * * /what/a/way_to/make_a_livin.sh