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.

I use crontab -e in ubuntu server for running a backup.sh file every night at 02:00 AM. I wrote: 00 02 * * * root /home/backup.sh Then /etc/init.d/cron restart When do crontab -l it shows:00 02 * * * root /home/backup.sh. I can run backup.sh myself and It works fine. But it did not work automatically by schedule.

share|improve this question

7 Answers 7

When you run your cron jobs as user jobs (editing jobs with crontab -e) you don't specify the user. If you run system cron jobs, then you can specify user.

Either put that line to /etc/crontab or /etc/cron.d/yourfile using some editor like vi or nano Or remove 'root' before /home/backup.sh

Additional note is to make sure that you have all your programs like mysqldump in your script either with full paths or make sure that they are in your scripts execution environments PATH (which is different from you shells PATH). See:

man 5 crontab

EXAMPLE CRON FILE

EXAMPLE SYSTEM CRON FILE

share|improve this answer
    
I write PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin in first line. and change 00 02 * * * root /home/backup.sh to 0 2 * * * /home/backup.sh. It is working. thnx –  user963587 Jan 21 '12 at 12:54
    
I'm also quite new to stack sites, but I think you should mark answer accepted if it solved your problem. –  Manwe Jan 21 '12 at 16:30

Redirect the error messages to a log file and check that.

/home/backup.sh 2>1 /home/backup.output

It's very likely a right or path problem.

share|improve this answer

There are two points to talk about:

  1. You don't need to restart the cron daemon when installing a new cron job.
  2. The most common reasons of failing cron jobs are user privileges/permissions and environment variables. So, you need to make sure you are running the cron job under the same user. Also, try to change the commands used by including the full path.
share|improve this answer

Here's what comes to my mind:

  1. Are you sure cron daemon is running? Check it.

  2. If it is, is should log all jobs it executes, usually you can find it in /var/log/messages. Check to see if it is really tries to execute this command at that time.

  3. Check cron script permissions.

  4. If so, the problem is probably different environment - jobs executing under cron may have different $PATH (so you need to specify full path to commands in your script). Cron should mail error (and actually any) output of jobs its running. Check /var/mail/root file or setup mail alias root -> yout@email in /etc/aliases.

share|improve this answer

Try editing crontab in this way :

0 2 * * * root /home/backup.sh

and make sure to give execution permission to the script.

share|improve this answer

You don't need the 'root' part. That's only for /etc/crontab.

Try this instead:

00 02 * * * /home/backup.sh.

share|improve this answer

And make sure your crontab has a carriage-return at the end of the line that's supposed to run. This is a known bug in many Cron implementations that prevents the last line of the file from executing.

Better yet, just put a comment at the bottom of your crontab file with a '#' character.

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.