Wednesday, January 19, 2011

Crontab/Cron analyzed - not working issues

It has now become routine job to write shell scripts to schedule jobs on server. (Ubuntu)

Although its routine, still sometime we got stuck with same issues. so I thought lets write a small article
to type out common errors I made.

1) Path -
By default, in cron environment, no Path is defined, so you can not even run simplest of shell file with simple
command like 'date'

you need to define
PATH=/home/sandip/bin:/bin:/sbin:/usr/bin:/usr/sbin/$PATH

your script testCronFile.sh can reside in /home/user/bin. it should have 755 perms.
chmod 755 testCronFile.sh


additionally to make sure that Script is executing.

you can do it using following 2 ways..
1) sudo tail -f /var/log/syslog
You would see something like following..

Jan 19 10:08:01 localhost CRON[11593]: (root) CMD (testCronFile.sh)


2) In testCronFile.sh, You can include line like

echo "Testing Shell file" >> /home/sandip/cron.log

so after execution of script, cron.log file would get created with "Testing Shell file" written in it.

for accessing files under root permission from cron, use sudo
$ sudo crontab -e


You can add comments, if you have something else regarding cron files.

No comments: