0

I created a script that checks the process of apache server and sends an email if the process is down.

apache_down=`service httpd status | grep "inactive (dead)"`
echo “$apache_down" | mailx -s “APACHE_DOWN_`hostname`” [email protected]

The script works fine when run manually - it sends the mail with the correct subject and body "$apache_down".

However when I place it under crontab, it sends the mail only with the subject and not the body.

*5 * * * * script.sh >/dev/null 2>&1

The body is empty with just "" . I used mail -s and same result. Is this an issue with my script or crontab or mailx set up.

1 Answer 1

0

Check the $PATH variable if its properly set for executing commands , if not then set it in your script or generally for cron.If $PATH is not set properly for binary then , service command will not run or you have to add it manually by executing it as /usr/sbin/service

Also try to add the check if variable has some output inside script...

if [[ -n "$apache_down" ]]
then
echo “$apache_down" | mailx -s “APACHE_DOWN_`hostname`” [email protected]
else
echo "apache running fine" | mailx -s “APACHE_Running_`hostname`” [email protected]
fi
10
  • Yes, I do have a check condition for that. It works fine when I run the script on command line. Just that the crontab is sending the message with an empty body
    – Tony Jose
    Apr 14, 2020 at 19:37
  • can you try : mailx -s “APACHE_DOWN_hostname[email protected]<<MESSAGE $apache_down MESSAGE Apr 14, 2020 at 19:43
  • Try adding service with full path "/usr/sbin/service" and try print the variable output in a temp file and check what it holds... Apr 14, 2020 at 19:45
  • I modified it to the following: mailx -s “APACHE_DOWN_hostname[email protected] <<MESSAGE $apache_down MESSAGE Getting a syntax error : warning: here-document at line 12 delimited by end-of-file (wanted `MESSAGE') May be I am missing something.
    – Tony Jose
    Apr 14, 2020 at 21:33
  • Found that the issue was with the $PATH. I added the $PATH from the profile and it worked. Thanks for your help though.
    – Tony Jose
    Apr 15, 2020 at 23:37

You must log in to answer this question.

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