Tuesday, January 10, 2012

Cron Jobs and common issues with cron jobs


Cron jobs are commands that are run at a specified interval and hence they are difficult to troubleshoot. Cron searches its spool area (/var/spool/cron/crontabs) for crontab files (which are named after accounts in /etc/passwd); crontabs found are loaded into memory. Note that crontabs in this directory should not be accessed directly - the crontab command should be used to access and update them. The cron daemon runs every minute and checks for stored crontabs and commands to see if there are any that need to be run in that minute. If it finds one it is run. 


Common issues: 


1. If you are running a script via cron job which uses relative paths then your script won't be able to process those relative paths. You need to use complete path's. For e.g., if your script is trying to open a file abc.txt you should modify your script so that the file path is complete like: /home/myuser/abc.txt otherwise your script will not be able to find this file. 


2. Strict Permissions - Make sure all scripts/files/folders are given execute permissions. Use command chmod +x <filename> to give write permissions. 


3. To troubleshoot cron job you can modify your crontab file to include MAILTO=email@email.com with your email address. When cron job is executed the result/errors etc are sent to this email id and hence they are useful in troubleshooting. 


4. When cron job is run from the users crontab it is executed as that user. It does not however source any files in the users home directory like their .cshrc or .bashrc or any other file. If you need cron to source (read) any file that your script will need you should do it from the script cron is calling. Setting paths, sourcing files, setting environment variables, etc. need to be done from your script itself. 


5. If the users account has a crontab but no useable shell in /etc/passwd then the cronjob will not run. You will have to give the account a shell for the crontab to run.


6. If your cronjobs are not running check if the cron deamon is running. Then remember to check /etc/cron.allow and /etc/cron.deny files. If they exist then the user you want to be able to run jobs must be in /etc/cron.allow. You also might want to check if the /etc/security/access.conf file exists. You might need to add your user in there.


7. Cron does not deal with seconds so you can't have cronjob's going off in any time period dealing with seconds. Like a cronjob going off every 30 seconds.

No comments:

Post a Comment