Monday, May 18, 2015

Find out why cron is not running my job

Cron jobs are commands that your service runs at a specified interval and, as such, can be difficult to troubleshoot.

Some of the most common cron mistakes are:

1.     Using relative paths. If your cron job is executing a script of some kind, you must be sure to use only absolute paths inside that script. For example, if your script is located at /path/to/script.phpand you're trying to open a file called file.php in the same directory, you cannot use a relative path such as fopen(file.php). The file must be called from its absolute path, like this: fopen(/path/to/file.php). This is because cron jobs do not necessarily run from the directory in which the script is located, so all paths must be called specifically.
2.     Permissions are too strict. Please be sure all scripts, files, and folders that are being used are set to executable. In the case of writing to a file or folder, it MUST be writable.
In your server's shell, this is the command that will make a file executable:
chmod +x <file>
3.     Not specifying what type of file you are running. For instance, if you are trying to run a PHP script via the cron job, you must specify that the file being run requires the PHP language to run it. For instance, if the file is /directory/script.php, the cron should read php /directory/script.php. This goes for all scripts, regardless of the interpreter.


Troubleshooting Skills
1.     Add a job that job that will be executed every minute.

·    * * * * * echo  `date` `pwd` >> /home/userid/out.txt
2.       And then reload the Crond, make the job take effect.
Check the output file of the job, the file out.txt will be updated every minute.
If the file is not generated, please restart crond.
service crond restart
3.       If the time is different to what you see by run “date” command on your shell, please

Run the following command to check configuration of you server.
echo $TZ
ll /etc/localtime
Cron uses the local time. /etc/default/cron and other TZspecifications in the crontab just specify what TZ should be using for the processes started by cron, it doesn't impact the start time.
So if your localtime is UTC, like this
lrwxrwxrwx 1 root root 23 Mar  9  2014 /etc/localtime -> /usr/share/zoneinfo/UTC
And the TZ environment of your shell is others, you should please change system timezone. Note: setting the TZ variable is a temporary solution, and might act as a "mask" sometimes.
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
or
ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
And then restart the service
service crond restart
4.       If the problem still exists, please check log file for more information.

cat /var/log/cron
About the author

Williams Voon, devote to excel reporting solution. Product: GTD Excel Report Server.

No comments:

Post a Comment