Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

OS : Ubuntu 12.04

Now I want to use Backup and Whenever gem to automatic backup my database. When I connect the server by ssh as an added user to run backup perform -t my_backup,it works well.But the cron file:

0 22 * * * /bin/bash -l -c 'backup perform -t my_backup'

can't run at 22:00. When I use cat /etc/crontab check the cron's config file,it is:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

The /bin/bash and /bin/sh are different.What's the reason?How to do?

share|improve this question
    
What do you call “the cron file”? Is that the output of crontab -l? If you add a job * * * * * echo test, do you receive an email every minute (remember to turn it off after a couple of minutes)? –  Gilles Sep 5 '12 at 22:00
    
I have add MAILTO=<my_email_address>,but haven't receive a mail.Even added the * * * * * echo test. –  Jingqiang Zhang Sep 6 '12 at 1:47

3 Answers 3

up vote 1 down vote accepted

The special crontab file /etc/crontab has a slightly different format in that the sixth field must be the user the cron job will be run as. So if you want to put your job into that file, you must insert a user name between * and /bin/bash to match the format. Something like:

0 22 * * * root /bin/bash -l -c 'backup perform -t my_backup'

replacing root with whatever user name you really want to use.

share|improve this answer
    
yep, either that or JZ should add it to the user's own crontab. and for completeness, crontab files in /etc/cron.d/ have the same format as /etc/crontab. –  cas Sep 5 '12 at 3:55
    
Thank you very much.I have run whenever with rails user which I created by root.I have try your method to add root or rails user name between the * and /bin/bash,but it still doesn't work:-( –  Jingqiang Zhang Sep 5 '12 at 4:05
    
@JingqiangZhang If the command is running and failing, cron should be mailing the user the error messages. Also, cron itself should be logging its activities somewhere, probably in /var/log. –  Kyle Jones Sep 5 '12 at 4:32
    
The log file /var/log/cron.log has: Sep 3 22:00:01 domain-name CRON[20833]: (rails) CMD (/bin/bash -l -c 'backup perform -t my_backup'),I don't know why there isn't any result.Also,I didn't receive a mail by cron. –  Jingqiang Zhang Sep 5 '12 at 5:24

i also experienced the same few weeks ago. i was trying to copy and overwrite the crontab file. even though all the parameters were proper, cron was not running as expected. when i removed and copied the same file, it was working fine.. just try the same..

however i did not understand the reason it did not work when i tried to overwrite that file..

share|improve this answer
    
Thank you for your suggest,but I have no luck. –  Jingqiang Zhang Sep 5 '12 at 6:19
    
The safe way to make changes to the crontab is through crontab -e, which will open an editor for you and afterwards reload the config. cron is under no obligation to monitor if changes have been made behind its back. –  Ulrich Schwarz Sep 5 '12 at 6:45

Try to build a script with the backup command you want to execute, and then call this in the crontab like:

0 22 * * * (cd <absolute-path-of-directory> && ./script.sh >>script.log 2>&1)

In this script I recommend first of all a line like

#!/bin/bash
echo "`date '+%Y%m%d %H%M%S':`: $0 started"

So you can see that cron is executing it and concentrate on the command. Reasons why scripts don't run under cron that are normally executing fine are often find in the different environment that is used under cron.

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.