2

this is my cron entry:

*/5 * * * * /home/user/scripts/backupDB.sh && echo "Backup Successful: $(date)" >> /home/user/tmp/mybackup.log

This is the script that is called:

#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
date_now=`date +%Y_%m_%d_%H_%M`
dir_name="BU/db_backup"
tar_name="db_backup_${date_now}.tar.gz"
file_name="${dir_name}/${tar_name}"

if [ -z "$dir_name" ]; then
    mkdir $dir_name
fi

log() {
    echo $1
}

do_cleanup(){
#    rm -rf db_backup* 
    log 'cleaning up....'
}

do_backup(){
    log 'snapshotting the db and creating archive' && \
    mongodump -o ${HOME}/${dir_name} && tar -cjf ${HOME}/${tar_name} ${HOME}/${dir_name} || \
    log 'data backd up and created snapshot'

    log 'saving the backup archive in amazon S3' && \
    s3cmd put --acl-private ${HOME}/${tar_name} s3://bucket-name/db-backups/${tar_name} && \
    log 'data backup saved in amazon s3'
}


do_backup && do_cleanup

The thing is, this script works from the command line, but not from crontab. I took a look at Reasons why crontab does not work, and I have done everything that is mentioned there.

Is there anyway of running it via cron?

EDIT: I am currently running as su -c user Does that pose a problem?

6
  • Did you put that && echo part in after it didn't work? I wouldn't put that in just in case. Did you put this in your system cron or your user cron? Can you explain how you're running it from the command line? is the script executable? Feb 2, 2011 at 17:19
  • Ill try removing the && echo from the crontab and try again. Its in the user cron. TO run it from the command line I type ./scripts/backupDB.sh. Also works with bash scripts/backupDB.sh Yes, the script is executable, and has -rwxr-xr-x permissions. It is owned by the user, and belongs to the same group as the user. I also tried running it as root, and it still didnt work. Feb 2, 2011 at 17:24
  • If you want to echo something to verify the script is running, you could put the echo in the shell script itself to avoid issues on the command line.
    – pjmorse
    Feb 2, 2011 at 19:56
  • if [ -z "$dir_name" ]; then mkdir $dir_name; fi This means: If the dir_name variable is empty or unset, run mkdir with no arguments. That's obviously not what you want. Just do: mkdir -p "$dir_name" || exit instead. How are you determining that the script does not run exactly?
    – geirha
    Feb 3, 2011 at 1:55
  • 1
    @jgbelacqua I ran the script on the slave running CentOS, it worked :) The only conceivable diff is that I ran as root and tried as a regular user on Debian, and as a regular user on CentOS. Feb 7, 2011 at 14:05

2 Answers 2

1

Try changing your cron command to

bash -c '/home/user/scripts/backupDB.sh && echo "Backup Successful: $(date)" >> /home/user/tmp/mybackup.log'

I have found wrapping things in bash has helped setup environment values in the past. You might even be able to dump parts of your script as a result.

3
  • It did not work. Is there any particular reason you might have heard or ran across that a cript runs fine from the terminal, and not from the crontab? Feb 2, 2011 at 17:35
  • Usually an environment issue, like inherited PATH, etc.
    – belacqua
    Feb 2, 2011 at 23:05
  • It works, but I had a problem with the distribution. I changed to CentOS, and it works now. Feb 15, 2011 at 3:52
-2

I think, there is a problem with script permissions. Have you tried giving executable permission to your sh file? I was facing same issue. When I run through command prompt using sh command, it ran successfully. But, in crontab, it was not running. Then, I have provided 777 permission to script and it worked through crontab. It seems the problem is with Linux.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .