3

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
user9517
  • 115,471
  • 20
  • 215
  • 297
RonnieT
  • 133
  • 1
  • 1
  • 3

2 Answers2

10

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
user9517
  • 115,471
  • 20
  • 215
  • 297
  • 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. – user9517 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` – user9517 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
1

run

service crond status

to see if cron is active.

Laurentiu Roescu
  • 2,266
  • 17
  • 17