Terminal And Cron Job Return Different Status Code

Dear All,

I want to write a shell script to test the server is running or not. The method is pinging the server and examine the status code.

the script:

#/bin/tcsh

ping -c 3 host
if ($? != 0) then
actionA
endif

This script work fine in terminal. The status code is 0 when the host is running and 2 when the host is down.

But when I run the script in cron job, the status code is only returned in 1, no matter the host is up or down. Did I do something wrong in the script?

Thanks for your attention and help.

where is ping?

add an "echo $PATH" at the beginning of your script, make sure ping is in the path when run as a cron job.

Also, check your individual system and see what the default shell is when cron in running. Be explicit on your shell in the first line of the script with a "#!/usr/bin/sh"

Here's what I am thinking - if PATH isn't right, ping won't be found, returning an error code of 1.

if you are running some weird shell that is different than the interactive one, that also could cause the format for the if the be wrong.