I have a bash script set to copy a backup from a remote drive; and this script works fine if I run it from the terminal, i.e., $ ./getBackup
; however, if I try to put it in my crontab, it doesn't work.
I've read through a bunch of questions/answers here, including
- Cannot run bash script from crontab when it works from command line bash
- the script runs from command line but crontab fails
- Script does not run under cron but runs manually
Maybe I missed something, but I think I've tried most of the suggestions offered in those, and just about everything else I could think of. I've tried to run it from my crontab, from the sudo crontab, from /etc/crontab; I've checked the env of the terminal (where the script manually works) and put that into the script for the cron job.
Any one have any ideas?
[edit to add] Several commenters asked for the code in question, so:
crontab:
30 9 * * * /home/opsmonitor/Documents/getBackup
getBackup
cd /home/opsmonitor/Documents/
OF=backup-$(date +%Y%m%d)
echo "Retrieving backup file $OF"
scp root@IP ADDRESS HERE://opt/backups/backupsbydate/$OF.tgz $OF.tgz
As I said, getBackup works fine from the command line; and run from the crontab, it doesn't do anything. (Now that I redirected the output to a file, the echo does print to that file.)
Since scp runs from the command line, I thought it was an env issue, but as I said, I added a lot of the env variables to the script--and it still runs fine from the command line and fails from the crontab.