Showing posts with label crontab. Show all posts
Showing posts with label crontab. Show all posts

Tuesday, August 18, 2009

Cron Jobs Not Launching

We had an issue recently with cron jobs not firing on our Solaris 10 server. We finally tracked it down to cron not being initialised when the server rebooted. The error was in a file called /var/svc/log/system-cron:default.log and indicated that it couldn't find the user. Possibly due to NFS not being mounted.
sharfah@starship:~> tail -10 /var/svc/log/system-cron:default.log
[ Aug 16 00:01:25 Stopping because service disabled. ]
[ Aug 16 00:01:25 Executing stop method (:kill) ]
[ Aug 16 00:07:27 Enabled. ]
[ Aug 16 00:07:28 Executing start method ("/lib/svc/method/svc-cron") ]
[ Aug 16 00:07:28 Method "start" exited with status 0 ]
! No such user as sharfah - cron entries not created Sun Aug 16 00:07:28 2009
We then got the Unix SAs to restart svc:/system/cron:default:
sharfah@starship:~> tail -10 /var/svc/log/system-cron:default.log
[ Aug 18 17:25:19 Stopping because service restarting. ]
[ Aug 18 17:25:19 Executing stop method (:kill) ]
[ Aug 18 17:25:20 Executing start method ("/lib/svc/method/svc-cron") ]
[ Aug 18 17:25:20 Method "start" exited with status 0 ]
Another way to fix this issue, is to edit and resave crontab.

For debugging cron issues, a useful tip is to look at /var/mail/user which will contain any stdout or errors from your cron jobs.

Related post:
Percent Sign in Crontab

Saturday, May 09, 2009

Percent Sign in Crontab

From the man pages of crontab:

The sixth field of a line in a crontab file is a string that is executed by the shell at the specified times. A percent character in this field (unless escaped by \) is translated to a NEWLINE character.

Only the first line (up to a `%' or end of line) of the command field is executed by the shell. Other lines are made available to the command as standard input. Any blank line or line beginning with a `#' is a comment and is ignored.

This means that you need to escape any percent (%) characters. For example, I have a daily backup cron which writes the current crontab to a backup file every morning, and I have to escape the date command, as shown below:

01 07 * * * crontab -l > /home/user/cron.`date +\%Y\%m\%d`
Also note, that cron isn't clever enough to expand the tilde (~) character, so always use the full path to your home directory.

If you find that a cron hasn't fired, check your email in /var/mail/user.