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.
7 Answers
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
-
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 Jan 21, 2012 at 12:54
-
I'm also quite new to stack sites, but I think you should mark answer accepted if it solved your problem.– ManweJan 21, 2012 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.
There are two points to talk about:
- You don't need to restart the cron daemon when installing a new cron job.
- 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.
Here's what comes to my mind:
Are you sure cron daemon is running? Check it.
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.
Check cron script permissions.
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.
Try editing crontab in this way :
0 2 * * * root /home/backup.sh
and make sure to give execution permission to the script.
You don't need the 'root' part. That's only for /etc/crontab.
Try this instead:
00 02 * * * /home/backup.sh.
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.