0

I have no idea why this script is not working. The script has been made executable and I get no output in mail or the snap.log that I set up to catch anything. I've run as root at the command line and it worked perfectly, but once it's been added to cron, nothing happens. I'm completely stumped and any help would be appreciated. Thanks.

Running on OS X Mavericks.

#!/bin/bash

# Ignores white space in directory names
IFS=$'\n'

# Sets all variables needed
export DLY_BKP=/Volumes/BKP/Daily_bkp.`/bin/date +%m%d%y`
export DLY_LOG=AAAdaily_bkp.txt
export BKP_DIR=/Users/Backup_logs
export VMPATH=/Applications/VMware\ Fusion.app/Contents/Library


#####################################################

# Finds all running VMs
$VMPATH/vmrun list | /usr/bin/sed 1d > $DLY_BKP/$DLY_LOG

# Creates snapshots of all running VMs on the server
while read vm; do
    $VMPATH/vmrun -T ws snapshot $vm "snapshot `/bin/date +%m/%d/%Y`"
    $VMPATH/vmrun -T ws deleteSnapshot $vm "snapshot `/bin/date -v-2d +%m/%d/%Y`"
done < $DLY_BKP/$DLY_LOG > $DLY_BKP/snap.log 2>&1
nickg
  • 137
  • 1
  • 2
  • 12
  • Chances are that `cron` is executing the script using `sh` and not `bash`. – devnull Apr 14 '14 at 18:58
  • It looks like the snap.log isn't even being created when the job is supposed to run. Is there something that would cause it not to execute? Right now I'm using $ at now +1 /path/to/script/script.sh – nickg Apr 14 '14 at 18:59
  • Doesn't the shebang line force it to use bash? – nickg Apr 14 '14 at 19:00
  • You have a `2>&1` after the loop, but not on the `$VMPATH/vmrun list ...` line. Try appending a `2>&1` to that line. Maybe it's dying there before it even gets to the loop. – Mike Holt Apr 14 '14 at 19:03
  • @nickg depends on how the cron job is set-up. but it's unsafe to rely upon it. – devnull Apr 14 '14 at 19:05
  • It's not the line that doesn't have 2>&1 because this is actually a snippet at the beginning of a larger script. This is the only part that doesn't work. – nickg Apr 14 '14 at 19:37
  • Follow the debug guide on the [crontab tag wiki](http://stackoverflow.com/tags/crontab/info) and come back with the results. – that other guy Apr 14 '14 at 23:18
  • Well I'm an idiot, I just needed to mount the backup drive BEFORE executing this part since the log file the snapshots use resides there. It's working now. – nickg Apr 19 '14 at 13:12

1 Answers1

1

Setting up scripts for CRON use usually requires adding paths via crontab setup or within the script itself. Try adding an explicit PATH to either your crontab OR to your script.

Crontab entries vary by OS so check your docs - for Free BSD they are something like 'crontab -e' and edit entries (see 'man crontab' or 'man -a crontab' for specifics for your OS):

PATH=/some_custom_path # affect all JOBs in Crontab

 ### MM HH Day Month WEEk_Day << Order VARIES by OS!  
 ## run on 2nd/4th/6th days of week  
19 9 * * 2,4,6  /home/user/bin/some_script  

Throw all output away:

19 9 * * 2,4,6  /home/user/bin/some_script > /dev/null 2>&1  

Send all data to log file you specify:

19 9 * * 2,4,6  /home/user/bin/some_script > /tmp/cron_job.log 2>&1  

Mail results & Errors

19 9 * * 2,4,6  /home/user/bin/some_script 2>&1 | mail -s "Cron results" user@your_dom.com 
Dale_Reagan
  • 1,953
  • 14
  • 11