cron's irregular responds

hi,
i ve constructed a script to monitor the crond ,if the crond service is not available/not running then a mail ll be sent the mention mail id.. here the code

crond=`service crond status|grep pid |awk '{print $5}'|cut -c1-7`
if [ "$crond" != "running" ];
   then
   echo -e "the CRON jOb is currently offline" | mail -s " ******CRONTAB ALERT*******" srini@yyyyyyyyy.com
fi

then i ve given the entry for cron as

* * * * * /usr/local/myscripts/cronstatus.sh 

Now the issue is though the crond is running me getting the alert each minute. Where im going wrong :wall:

Add set -x at the beginning of your script.
Use following line in crontab:

* * * * * /usr/local/myscripts/cronstatus.sh >> /tmp/mycron.log 2>&1

My guess is, that there will not be "running" in that $crond, since you don't have PATHs etc. available when starting scripts via cron. You have no environment active so commands like service, cut, awk, grep... will not be found and so with produce some errors where the test fails.
After a run there should be a log file /tmp/mycron.log where the output can be analyzed.

Excuse me if, but are you trying to monitor cron by using cron? If crond isn't running, neither will your script, and you won't get any notification anyways.

Oh lol good point :b: Time to go home heh :wink:

hi zaxxon,thanks for ur comments and then U r rite , i got some error in the logs as

+++ service crond status
/usr/local/myscripts/cronstatus.sh: line 3: service: command not found
+++ awk '{print $5}'
+++ cut -c1-7
++ crond=
++ '[' '' == running ']' 

now what can i do , now to get the environment .. Myself newbie to the scripting provide some solutions..

---------- Post updated 05-20-11 at 12:57 AM ---------- Previous update was 05-19-11 at 11:35 PM ----------

fine pludi .. valuable point , let assume the same code for httpd service now whats your solution??????:mad: may i know that?

For httpd it would work, as long as cron is running. The path issue aside your script already does what it should do, you only have to hard-code the complete path to the service command ( which service ). But it's just not possible to monitor if a program is running from within that program. Think about it this way: if you are alive, you can tell if someone else is alive, and you know that you yourself are alive. But if you're dead, you have no way of telling that to anyone.

ok ok got ur point ... could u suggest me some other script to monitor the httpd service and send me alert when the service is offline...

Read my post again. It IS completely feasible to monitor httpd using your script and cron, the ONLY thing not possible is to monitor cron using cron.

that's possible rite??? but me still facing the same issue which i faced in crond (i.e) though the service httpd is online there is alert for offline of service..:eek:

The output of the service command isn't fixed, as far as I know, but highly dependent on the script in /etc/init.d. Meaning: service httpd status could report the status in a different way than service crond status or service sshd status . It might be possible to check the return code instead, but I have neither a RHEL, CentOS, SuSE, or Fedora installation available to check this.