1833861 Members
2077 Online
110063 Solutions
New Discussion

Setting up cronjob

 
SOLVED
Go to solution
Sanjay Verma
Super Advisor

Setting up cronjob

Hi Friends,
I've setup a cronjob to stop and start the webserver at a schduled time. Now I would like to setup so that if the webserver is not able to start properly, it should send an email notification to me. Will appreciate for your help on this.

The crontab entry is as excerpted below:
0 02 * * * /users/skverma/stop_web > /dev/null 2>&1
5 02 * * * /users/skverma/start_web > /dev/null 2>&1

Regards,
Sanjay
Co-operation - The biggest chain reaction
10 REPLIES 10
John Poff
Honored Contributor
Solution

Re: Setting up cronjob

Hi Sanjay,

I'd suggest adding some code to your 'start_web' script. I don't know what command you run in the script to start the web server, but if the command gives you a non-zero return code if there is a problem you could trap for it and send yourself an e-mail. Something like this:

web_start_command
RC=$?
if [[ RC -gt 0 ]];
then
echo "Problem starting web server" | mailx -s "Web server start problem" sanjay@yourmail.com
fi

If you don't get a valid return code from your command, you could check for a process and send an e-mail if that process isn't running.

Just a couple of ways to try it.

JP

Sridhar Bhaskarla
Honored Contributor

Re: Setting up cronjob

Hi Sanjay,

Modify your start_web script and append the following.


your_startup_web
..
..
HTTPDS=$(ps -ef|grep httpd|grep -v grep |wc -l|awk '{print $2}')

if [ $HTTPDS = 0 ]
then
echo "WEB Services did not start on $(hostname)" |mailx -s "$0 failed" yourid@yourdomain.com


The above script will work provided the daemons run as httpd. If not, replace httpd with the appropriate daemon name in the grep command.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sanjay Verma
Super Advisor

Re: Setting up cronjob

Hi Sridhar & John,
Thanks for your quick response. I've only one line in the script as excerpted below:

cd /opt/psoft/weblogic/6.10/config/domain.sanjay;nohup startPIA.sh &

Now the question is, if I need to start the Weblogic for multiple domains, i.e., domain.sanjay, domain.office, domain.home etc. and I've setup the script like below, how am I going to identify the process related with each domain?? Or you can suggest the best way to do this.

cd /opt/psoft/weblogic/6.10/config/domain.sanjay;nohup startPIA.sh &
cd /opt/psoft/weblogic/6.10/config/domain.office;nohup startPIA.sh &
cd /opt/psoft/weblogic/6.10/config/domain.home;nohup startPIA.sh &
Co-operation - The biggest chain reaction
John Poff
Honored Contributor

Re: Setting up cronjob

You could use Sri's solution and check for it after each web server is started. After the first one starts you could e-mail yourself if there is less than one process running. If not, start the second one, do the test, and e-mail yourself if there are less than two running, and so on. Of course, if there is a unique process to identify each of the three web servers it makes it a lot easier.

JP
Sridhar Bhaskarla
Honored Contributor

Re: Setting up cronjob

Hi (Again),

Make this as a function like this that can take arguments.

//
check_web()
{
PROC=$1
NUM_PROCS=$(ps -ef|grep $PROC|grep -v grep |wc -l|awk '{print $2}')

if [ ${NUM_PROCS} = 0 ]
then
echo "WEB Services did not start on $(hostname)" |mailx -s "$0 failed" yourid@yourdomain.com
fo
}

cd /opt/psoft/weblogic/6.10/config/domain.sanjay;nohup startPIA.sh

check_web (proc_name_for_sanjay)

cd /opt/psoft/weblogic/6.10/config/domain.sanjay;nohup startPIA.sh &

check_web (proc_name_for_home)

etc.,

//

There are two deals here

1. You will need to have a process that is unique for each domain.
2. You will have to put a 'sleep x' after each startPIA.sh command. Or take out the &.


BTW how do your processes look like?.

-Sri


You may be disappointed if you fail, but you are doomed if you don't try
Sanjay Verma
Super Advisor

Re: Setting up cronjob

Hi Sri,
The following is the content of the script that I've created but does not seem to be working fine. Am I making mistake somewhere?
Once this's implemented I'll go through creating the proc as indicated.

#!/bin/sh
COUNT=$(lsof | grep LISTEN | grep 50 | wc -l)
if [ $COUNT != 16 ]
then
echo "All WEB Services did not start on $(hostname)" | mailx -s "WebServer Process Failed to Restart" skverma@hotmail.com
fi
exit

When tried to execute this manually, after pressing CTRL+D after "fi" it gives mesg "EOT
Null message body; hope that's ok".
Co-operation - The biggest chain reaction
Sridhar Bhaskarla
Honored Contributor

Re: Setting up cronjob

Hi Sanjay,

1. Why do you want to use lsof?. Use ps. lsof can hang or take a lot of time as it has to probe a lot. Since it was hanging (or still running), you had to press ctrl-D
2. Replace /bin/sh with /usr/bin/ksh. Depending on how your system is setup, you may be using old bourne shell.


-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Sanjay Verma
Super Advisor

Re: Setting up cronjob

Hi Sri,

Pl. find my comments below:

(a) I need to identify which ports are up so through lsof I count the rows and thus identify if all the ports are up and running properly.
(b) /bin/sh is defined in all other scripts are is working fine.
(c) If I break down the script, the exact position where it's hanging is:

#!/bin/sh
---ok
COUNT=$(lsof | grep LISTEN | grep 50 | wc -l)
---ok
if [ $COUNT != 16 ]
---ok
then
---ok
mailx -s "WbServer Process Failed to Restart" myemail@xyz.com
---Hangs (Whereas if I execute the mailx command manually on the command line it works fine)
fi
exit
Co-operation - The biggest chain reaction
Sridhar Bhaskarla
Honored Contributor

Re: Setting up cronjob

Hi Sanjay,

Put only that mailx sentence in the script and execute and see if it works.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sanjay Verma
Super Advisor

Re: Setting up cronjob

Hi Srini,
It all works now. The mailx was waiting for the body text and that's the reason why it was hanging up. Now, I've included echo... and then piped to mailx.

Also, I am interested in creating the function. If it's ok you can send any relevant doc. at skverma786@rediffmail.com.

Thanks once again for all your suggestions.

Cheers,
Sanjay
Co-operation - The biggest chain reaction