Crontab not working [closed]

Tag: linux , ubuntu , crontab Author: aaadongdong Date: 2011-09-20

I use crontab -e to add the following:

36 * * * * /root/Backup/backup.sh

But it does not work!

I find this in syslog, but it actually does not run:

Oct  6 15:36:01 ubuntu CRON[7685]: (root) CMD (/root/Backup/backup.sh)

I sure my script works as it runs successfully if I run it directly.

# 
# define a function to create directory
# 
fnCreateFolder()
{
        if [ ! -d $1 ]; then
                mkdir $1
                echo "New directory, '$1', is created"
        fi
}
############################################################################
#
# Full pathname to binaries to avoid problems with aliases and builtins etc.
#
DATECMD="/bin/date"
RMCMD="/bin/rm"
subdir=`${DATECMD} +%Y-%m-%d`
if [ ! -d "$subdir" ]; then
        fnCreateFolder "$subdir"
fi
############################################################################
        subdir=`${DATECMD} +%Y-%m-%d`
        fnCreateFolder "$subdir"
        datetime=`${DATECMD} +%Y-%m-%d_%Hh%Mm%Ss_%p`
        echo "date-time: $datetime"
        backupname="[email protected]$datetime.sql"
        # backup DB
        mysqldump -u'sun' -p'sun2010fyp' smartward > $backupname
        echo "start tar cvzf Backup($datetime).tar.gz $backupname"
        # zip the backup file
        tar cvzf "Backup($datetime).tar.gz" "$backupname"
        echo "finished zip $backupname as Backup($datetime).tar.gz"
        # move tar file to dest folder
        echo "& move it to the destination directory"
        mv "Backup($datetime).tar.gz" "$subdir"
        # & remove the backup file after zipping it
        ${RMCMD} -f "$backupname"
        echo "finally removed $backupname"
#DONE
Maybe your script depends on an environment variable that is present in your shell, but not in the shell started by cron?
But no Hello is printed when I replace it with echo... So I don't think it is not related to my script
Redirect the output from echo to a file: echo "Hello" > /tmp/hello.txt. That will prove to you whether the echo succeeded or not.
it success!!!Thx But it redirect the output of backup.sh, it only create the empty file....(I have echo statement in backup.sh)
is it a permission problem?

Other Answer1

two things you're missing:

  1. The script should start with #!/bin/sh
  2. The script should be executable: chmod u+x /root/Backup/backup.sh

Alternatively you could change your crontab entry to the following: 36 * * * * /bin/sh /root/Backup/backup.sh

Info: Either you invoke the script directly from /bin/sh or you need to give enough information to the shell (cron's environment) to execute the script with the correct interpreter. That's handled by item 1. Also, since you're attempting to execute the script directly it must be executable.

Other Answer2

Try piping stdout and stderr into a file. eg. /..../backup.sh >/tmp/backup-output 2>&1 and checking that. Also check that there is no environment issues between cron running it and you running it on the command line. Put env at the start of the file.

Also are you sure that Backup should start with a capital letter?

comments:

Yes, I am sure I paste this "/root/Backup/backup.sh" to shell and it run well

Other Answer3

Thank you all of you The problem is solved I use absolute path instead of relative path inside the shell script.