Below is my shell script which is written in another x to find the health of server y. ( I have not written in server y because there is no functionality of getting mails)

count=$( ping -c 5 $target | grep icmp* | wc -l )
if [ $count -eq 0 ]
 echo "The Tomcat Dev server GMP_Dev_Tomcat_cvgrhegmpd003 with ip address is DOWN Please check your server ASAP" |  mail -s " Dev Tomcat Server Status" [email protected]
   echo "The Tomcat Dev server GMP_Dev_Tomcat_cvgrhegmpd003 with ip address is UP and WORKING"


I am not getting any alert. I have added my shell script to crontab -e which runs every 1 min. But if I run the script ./scriptname.sh, I do get mails (I was checking when my server was up).

  • 1
    Since it's running as a cronjob, does it send any email to the user who has this cronjob? I would also start by changing all commands (wc, mail, grep, ping, etc) to use the absolute paths. If a script works from the command line but doesn't from crond then the difference is usually with the environment.
    – Bratchley
    Oct 31, 2014 at 12:43
  • @JoelDavis yes it does send the mail ( when I did a test run) ... But im not getting any alerts when its actually down. I cant give the absolute path. I just want to check if the server is alive. As of now my program pings to the target server and checks if there is reply..If not then it should mail ( but it doesn't) Oct 31, 2014 at 13:09
  • Your problem is the grep... actually the pipes....
    – Braiam
    Oct 31, 2014 at 13:13
  • @Braiam then what should be ? Oct 31, 2014 at 13:23
  • Not sure what was meant by pipes but if the user themselves is getting the email instead of you then that means $count is always not zero. I would update the paths. There's also a more straightforward way of doing this. Check this out.
    – Bratchley
    Oct 31, 2014 at 13:59

1 Answer 1


If it's failing in cron where it works in shell, it's usually an environment issue, as Joel mentioned.

In your cron, try logging the script output by appending >> /home/myuser/myscript.log to the end of your cron line, then read the file after your cron runs.

You may find a lot of entries like -bash: ping: command not found, in which case you need to use the absolute paths in your script (ie /bin/ping instead of ping). You can use which [command] to find the absolute path for a command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .