Bash script can't handle errors when run by cron
Hi,
I have a script that that is supposed to send me an e-mail when a host is not responding to ping: #!/bin/bash /bin/ping -c 1 -w 5 myhost.mynetwork.intra &>/dev/null if [ $? -ne 0 ] ; then /usr/bin/mailx -s "Server down " john.doe@domain.com < ./status.txt fi The script works fine when I execute it directly but when cron executes it, the ping error is never picked up by the script so the if statement is ignored. What have I done wrong? Crontab entry: */1 * * * * /home/user/scripts/checkstatus.sh >/dev/null 2>&1 |
I see nothing strange except you have to use the full path of status.txt, unless it is placed in the crontab owner's home directory. Also, for testing purposes, remove the redirection to /dev/null from the crontab. If the cron job spits out some error, it will be sent to the owner's mail and you can check it. Hope this helps.
|
I had to disable the cron tab out put because it was sending me one e-mail a minute.
I just can't figure out what's wrong with this one. |
Quote:
|
There's no need to background (&) the ping cmd either.
|
All times are GMT -5. The time now is 05:04 AM. |