I have a setup as below but its never getting executed.

$ chmod +x /var/tmp/myscript.sh
$ vim /var/tmp/crontab.sh;
* * * * * sleep(10); /var/tmp/myscript.sh
$ crontab /var/tmp/crontab.sh

Now just, noticed its not working. What am i doing wrong?

$ grep CRON /var/log/syslog
Nov 28 15:20:01 sun CRON[1768]: (sun) CMD (sleep(10);/var/tmp/myscript.sh)
Nov 28 15:20:01 sun CRON[1766]: (CRON) error (grandchild #1768 failed with exit status 2)
Nov 28 15:20:01 sun CRON[1766]: (CRON) info (No MTA installed, discarding output)

Follow up:

$ vim /var/tmp/crontab.sh
* * * * * sleep 10; /var/tmp/myscript.sh>/dev/null 2>&1
$ tail -f /var/log/syslog | grep CRON
Nov 28 15:50:02 sun CRON[2301]: (sun) CMD (sleep 10; /var/tmp/myscript.sh>/dev/null 2>&1)
Nov 28 15:51:01 sun CRON[2312]: (sun) CMD (sleep 10; /var/tmp/myscript.sh>/dev/null 2>&1)

sleep does not take the argument in parentheses (like in C), so it should be

* * * * * sleep 10; /var/tmp/myscript.sh

Always test the commands in the system shell before feeding it to cron ;).

  • 1
    Note also that cron reports the output ( which in this case would have been an error message telling you what you did wrong ) via email, so you should install a mail server such as postfix and check your local mail. – psusi Nov 28 '11 at 15:10

Actually, the best way to use crontab is by using crontab itself:

crontab -l # list current crontab entries

crontab -e # edit the cron table

As soon as you finish editing the crontab (via 'crontab -e'), it is activated, and will fire off whatever was set whenever it was set to fire off.

Note that the machine must be running for crontab to work. If you want something more flexible, you can look at anacron(8).

  • i use BASH to create crontab files, in such case you need to write your crontab informations in a file and the reload them using that file path. Thanks anyway! – user25165 Nov 28 '11 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy