Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What am I doing wrong here? Trying to set a cron job to run a shell script but it won't work. I entered the below after doing crontab -e. It does show up when doing contrab -l.

0 2 * * * root /root/localbackup.sh
share|improve this question
    
What do you mean by "it wont work"? –  Michael Hampton Oct 18 '12 at 17:07
    
wont work = cron doesn't run -- The script itself functions when hit manually –  RonnieT Oct 18 '12 at 17:31
    
Have you looked in the cron log? There should be an error message there that would say something like "root: command not found." which is explained by Iain's answer. –  Ladadadada Oct 18 '12 at 17:37

2 Answers 2

up vote 6 down vote accepted

You're using the crontab format that's used for /etc/crontab. For the user crontabs you don't need to specify the user

 0 2 * * * /root/localbackup.sh

should run.


Answering the commants

The output from vi is normal, crontab will copy it to the correct location

  • Check that cron is running
  • Check that there is a newline at the end of your command (crontab -e and press return at the end of the line)
  • Check the root account's mail for any output that is mailed from cron.
  • Capture the output of the command 0 2 ... &>/tmp/mylog.log
share|improve this answer
    
Still not running. Revised to do * * * * * /root/localbackup.sh to test. The path in VI editor states "/tmp/crontab.QUz4Io" 2L, 62C –  RonnieT Oct 18 '12 at 17:33
    
@RonnieT: updated my answer. –  Iain Oct 18 '12 at 17:52
    
Looks like I'm getting this error: /bin/sh: /root/localbackup.sh: /bin/sh^M: bad interpreter: No such file or directory –  RonnieT Oct 18 '12 at 18:17
    
@RonnieT: That looks like you edited the script using windows (^M is a give away). On the CentOS machine run dos2unix /root/localbackup.sh this will convert it to unix format. Also ensure that the first line of your localbackup.sh is #!/bin/sh –  Iain Oct 18 '12 at 18:23
    
Got it working. Cron was not the issue it was the formatting of the shell file. Thanks for your help! –  RonnieT Oct 18 '12 at 18:35

run

service crond status

to see if cron is active.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.