LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script can't handle errors when run by cron (https://www.linuxquestions.org/questions/programming-9/bash-script-cant-handle-errors-when-run-by-cron-695837/)

rosv 01-08-2009 07:54 AM

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

colucix 01-08-2009 08:28 AM

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.

rosv 01-08-2009 09:24 AM

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.

repo 01-08-2009 09:29 AM

Quote:

it was sending me one e-mail a minute.
Any pointers in the emails?

chrism01 01-08-2009 07:50 PM

There's no need to background (&) the ping cmd either.


All times are GMT -5. The time now is 05:04 AM.