Let’s assume you’ve setup a cron job in crontab and it doesn’t seem to be fired or not to do what you’d expect it to do.
There are a few things you should check:
- Is the cron job properly installed?
- Is cron working?
- Is the configured command working?
- Can cron run the configured command?
Is the cron job properly installed?
The first step is to check whether your cron job is at all installed. You will find useful commands in this previous post.
Check the time definition of the cron job
One issue could be that the time definition of the cron job is not valid or doesn’t trigger the cron job at the time interval you’d expect it to run. Checking this is pretty easy using the CRON tester. Just paste the time definition part in there and press “Test”. It will show you the date and time of the next 10 executions of the job.
If you e.g. paste the following: 1 2 3 * *
You’ll get the following output:
Given the CRON job starting at 2013-07-30 11:30:57 (now), it would run: 2013-08-03 02:01:00 2013-09-03 02:01:00 2013-10-03 02:01:00 2013-11-03 02:01:00 2013-12-03 02:01:00 2014-01-03 02:01:00 2014-02-03 02:01:00 2014-03-03 02:01:00 2014-04-03 02:01:00 2014-05-03 02:01:00
Is cron working?
The first thing you should do is to check the log files:
grep cron /var/log/messages
The results should look like this:
Jul 29 23:38:01 xxxxxx /usr/sbin/cron: (root) CMD (/bin/echo "hello" >> /tmp/test.log)
First the date and time of execution, the the hostname, the the path to cron and the process ID of the cron child process, then the user and after CMD the command executed.
If you issued a crontab withthe -l option you will also see such entries:
Jul 29 23:43:45 xxxxxx crontab: (root) LIST (root)
If you do not see anything in there, cron is probably not running properly…
In order to check whether cron is working, the easiest way is to install a cron job running every minute and giving you some kind of output i.e. either writing a string in a file or displaying a string on open terminals.
Write a string in a file
Install the following cron job (I assume you know how to install it, otherwise you’ll probably not have read this far…):
* * * * * /bin/echo "hello" >> /tmp/test.log
This will cause the job to run every minute and will write “hello” to the specified file. Make sure that the user has access to this file. Feel free to place the file somewhere else if required.
Then just sit and relax for a few minutes. After that you should see that the file contains “hello” a few times. This means cron is running and you can just remove this new cron job.
Display a string on open terminals
This can be done using the wall command. This command takes a file as parameter and prints the contents of this file to all open terminals. Alternatively, if no file is specified, it will print the standard input to all terminals. That’s the way we’ll do it. We’ll echo a string and redirect it to wall:
* * * * * /bin/echo "hello" | /usr/bin/wall
You should then see the following displayed every minute:
Broadcast Message from root@xxxxxx (/dev/pts/0) at 11:38 ... hello
Before installing it, please first check the location of wall on your system (on mine it is in /usr/bin):
# which wall /usr/bin/wall
If it works, you can just remove the new cron job.
Is the configured command working?
To check whether the command is working properly, you can just execute it from a shell. Of course the user and the environment might be different. In order to really test the script in a real cron environment, please refer to this previous post.
Can cron run the configured command?
If cron can run the configured command, you should first be able to see in /var/log/messages that it is being executed. If it is the case and you still do not get the expected results, you should check the rights on the script or executable being called.
Also a good way to find out what happens is to redirect the output of the called command to some file e.g.:
0 0 * * * /home/user/bin/myscript.sh >> /home/logs/mylog.log 2>&1
This will cause all output of the script to be redirected to the specified file. If you happen to call this command manually with a different user, please keep in mind that the permissions of the log file will be wrong (belonging to the wrong user) and after that the cron job might not be able to write to this file. If it is the case, you should just delete the log file created in the manual test or update its permissions.