Fun with Cron
For some of my Ubuntu Servers (10.04) the Cron jobs did not work or only sporadically executed. I’ve asked around, but none of my friends could explain why this is so. This happened on physical as also on virtual servers. Thus I set out today to finally figure out what caused this. To mention it upfront, I did not find the cause of it, but I got my Cron jobs working again. (and learned a lot along the way)
System Crontab and User Crontab
In the past I always set cron jobs in the /etc/crontab file. This is also the way I’ve used cron in the last couple of years. By switching to Ubuntu server 9.x this worked, but somehow since 10.04 they only worked sporadically. As explained, I don’t know why this is so, but moving those cron jobs into the user (in my case the root account) with “crontab -e” they started working again.
The only difference is that one does not need to pass the user the jobs has to run in with the command, since we edit the user crontab file with “crontab -e”, but everyting else works now as expected.
Getting cron to log
Under Ubuntu logging of any cron action is disabled by default. Since, I wanted to see if my jobs are running and execute successfully I had to enable it. Enabling logging of cron is done in the system default files with:
Uncommenting the ‘#’ before the line that says cron and it will enable logging for cron:
and save the file. Now simply reload or restart the rsyslog utility with:
service rsyslog restart
Note: In my case, I had to create the file “/var/log/cron.log” manually in order for cron to write the log.
Enable or preventing cron to send eMails
I guess by default, cron will send eMails on errors or completed actions to the user. In my case, I got a lof of eMails with completed actions and more from cron shortly after. Controlling sending of eMails is done by the global variable ” MAILTO=”” “. One can also direct the output to null for disabling sending of emails.
To disable cron from sending for all jobs:
Add MAILTO=”” to the top of cour crontab file.
To disable sending of eMails for individual jobs:
0 1 5 10 * /path/to/script.sh &> /dev/null