I have a very simple bash script that runs flawlessly when I do
./removeOldBackup.sh
or
sh /home/myusername/backup/removeOldBackup.sh
but when I add it to crontab like
* * * * * sh /home/myusername/backup/removeOldBackup.sh
or
* * * * * /bin/sh /home/myusername/backup/removeOldBackup.sh
it never works...
This is my script:
#!/bin/sh
find . -name 'files_20[1-3][0-9]-[0-9][0-9]-[1-2][1-9]--*' -delete
find . -name 'files_20[1-3][0-9]-[0-9][0-9]-0[2-9]--*' -delete
find . -name 'database_20[1-3][0-9]-[0-9][0-9]-[1-2][1-9]--*' -delete
find . -name 'database_20[1-3][0-9]-[0-9][0-9]-0[2-9]--*' -delete
This is my script permissions:
-rwxr-xr-x 1 root root 295 Jul 25 10:07 /home/myusername/backup/removeOldBackup.sh
Crontab is added for user root.
This is what I find in /var/log/syslog
:
Jul 25 10:11:01 myservername /USR/SBIN/CRON[7583]: (root) CMD (sh /home/myusername/backup/removeOldBackup.sh)
So again, when I run the script manually, my backup files get removed correctly. When it is run by cron, they never get removed. I'm using debian-6.0-x86_64.
find
runs on current working directory ie. when you run your script as./script.sh
, everything works fine. However, when cron runs your script, the current working directory is not what you expect it to be. Addcd /correct/path
in the beginning of your script or use an absolute path forfind
. – Jul 25 '14 at 08:43