1

I have a script that I wrote to verify on an OS X machine (v 10.7.4) if a volume is up or down. If it goes down, it is supposed to send an email. When I run the script from the command line, it works perfectly and shows me my control volume (DGS_Ima_ASDF_ges_MTL) as being down in the email I receive. However, if I schedule the script in the crontab, it says all my volumes are down, including my control volume, in the email that I receive. Can someone maybe tell me how to fix this. This is my script:

#!/bin/bash

array=("Chatelaine" "DGS_Ima_ASDF_ges_MTL" "FMC_MTL" "GF_MTL" "Holding_Tank_MTL" "LACM_MTL" "LAP_MTL" "actualite" "Loulou" "Loulou_Web" "MQC_MTL" "PS_MTL" "QP_MTL" "Visuels_MTL")

EMAIL_ADDRESSES=here.now@there.com
SAVE_TO=~/Documents/drives_not_mounted
CRONTAB_CONFIG=~/Documents/crontab_paramaters

rm $SAVE_TO

for counter in ${!array[*]}
do
   mount | grep ${array[counter]}
   if [ $? -ne 0 ]; then
       echo "${array[counter]}" >> $SAVE_TO
   fi
done

ls ~/Documents | grep drives_not_mounted
if [ $? -eq 0 ]; then
    mailx -s "Mounted volumes are down" "$EMAIL_ADDRESSES" < $SAVE_TO
fi

exit;
Sven
  • 98,649
  • 14
  • 180
  • 226
zdarma
  • 23
  • 3
  • (Qeuestion) Title =! Job title. – Sven Aug 09 '13 at 15:31
  • 1
    What user are you running the cron job as? Also, relative paths are always a bad idea in scripts. – cduffin Aug 09 '13 at 15:33
  • The user running the cron job is the same one I use when testing from the command line. It is also the same user I use for running a similar script that checks if a process has dropped. This script has no problems. The script that works, has the same relative paths. – zdarma Aug 09 '13 at 16:13
  • One difference when running as a crontab is that you don't source `/etc/profile`. Have you tried `. /etc/profile` at the top of your script or when calling the cron job? Does that change result for you? – Petter H Aug 09 '13 at 16:38
  • Adding the PATH=.... as suggested by minniux fixed the problem. – zdarma Aug 09 '13 at 17:21
  • You can iterate over the contents of the array directly (you don't show that you're using the index variable for anything else). Instead of using `PATH`, hardcode the path to common utilities (e.g. `/bin/mount`) for better security. Don't use `~` in scripts, it's meant to be a command-line convenience. Use `$HOME` instead. No need to `grep` `ls`: `/bin/ls $HOME/Documents/*drives_not_mounted*` instead. No need to test using `$?` in this case, you can test the result of the command directly: `if /bin/mount ...; then`. No need to `rm` your output file. Instead of `>>` use `>` after the `done` – Dennis Williamson Aug 09 '13 at 22:03

1 Answers1

2

First, add the following PATH line to your crontab file:

 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Second, configure cron to log the output of the script to a log file so you can further troubleshoot it, by adding the following next to the cron job line itself:

 &> /path/to/log.txt
minniux
  • 408
  • 2
  • 6
  • After looking at the output of the script in a log file, I saw that crontab did not recognize the command "mount". I added the PATH to my crontab and all is good. Thanks. I'd give you an UP on that, but my reputation isn't high enough. Thanks again. – zdarma Aug 09 '13 at 16:40
  • grep CRON /var/log/syslog since failed cron jobs will already make a log, there is no point in making a separate log file. – j0h Aug 09 '13 at 19:32
  • not in all systems, and not always detailed enough :) – minniux Aug 18 '13 at 14:19