LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Restart apache on crash (https://www.linuxquestions.org/questions/linux-software-2/restart-apache-on-crash-190187/)

brentos 06-05-2004 10:09 PM

Restart apache on crash
 
I have a copy of apache2 running under a user on RHE to serve up images for a website. The site has been experiancing heavy load lately and the apache2 occationally crashes, but if no one is around to notice it, it might stay out for a while or over night depending on when it crashes.

Is there a way to monitor apache and have it restart if it crashes? Sort of what cpanel does for its version of apache?

Thank for anything you guys can suggest.

SBing 06-06-2004 02:21 AM

You could add a script to cron.daemon to run every (15?) minutes (the time interval is up to you)
which runs 'ps aux | grep httpd' - if it can't find httpd then it 'apachectl start' :)

Good luck

Steve

j-ray 06-06-2004 08:37 AM

do u have any ideas why it crashes? u may take a look at the server logs to find out. apache does not tend to crash without reason as far as i know.

brentos 06-06-2004 11:30 AM

Yes it crashed because it gets maxed out on its connections for a extended period of time. We are running at around 12 million page loads a day and 1200 Gigs of bandwidth a month on that server and the apache2 is hosting the images.

The only question I have is can i get the grep to check the user name as well? Cause there is two copies of apache a 1.3 that comes with cpanel and a 2 to host layouts and images since the cpanel couldn't handle it all. So I just want to check the one running as a specific user not nobody.

Thanks for the help.

SBing 06-06-2004 01:11 PM

sure,

ps aux | grep httpd | grep -v nobody | grep -v grep

grep -v is an inverse grep,

the first grep -v ignores httpd running by nobody
the second grep -v ignores grep

or you could grep for the specific user

ps aux | grep -httpd | grep <user>

Steve

brentos 06-06-2004 02:22 PM

So just to make sure I did this right I put

if ps aux | grep -httpd | grep <user>
then
everything ok exit script
else
restart the server
fi

So that should be all i need right?

SBing 06-07-2004 02:29 AM

Essentially yes, in pseudo:

Code:


IF ps aux | grep -httpd | grep <user> = ""                #I.E. doesn't equal anything
THEN
    restart server
ELSE
    do nothing
END IF

Maybe you can post your final script for others to use :)

Steve

brentos 06-07-2004 10:54 AM

Here is my final script, I used a different version of the ps command just cause it required one less grep command so I figured less commands is better and I did the if backwards cause I could never get the = "" to work for some odd reason, I tried like 20 ways but hey this works. The only thing is when you run it manually you get a long list from the ps but hey this runs in a cron so I don't care I never see it. I also got it to send me an email when it happens so that I can sort of check up on the server, if its crashing to much its not that good. The restartMsg.txt just contains a sort little restart message.


#!/bin/bash
#script checks and restarts apache if required

if ps U layout | grep httpd
then
echo "Layouts server up and running"
exit 0
else
#restart server
echo "Layouts server down, restarting server"
cat restartMsg.txt | mail -s "Layouts failed on host.domain.com" root
/home/layout/bin/apachectl restart
echo "Layouts server restarted"
fi


Hope someone else might find the script useful, I know its small and easy to write but you never know.
Thanks Steve for the help :cool:

SBing 06-07-2004 02:41 PM

No problem, just glad you got it all sorted :)

Shame apache crashes though, still, heavy loads cause these sort of things to happen :(

Steve

Edit:

Quote:

Originally posted by brentos
The only thing is when you run it manually you get a long list from the ps...
I know it's not a problem since you're running it through cron, but for anyone else, try this instead:

if ps U layout | grep httpd > /dev/null

It'll suppress the output from ps :)


All times are GMT -5. The time now is 09:54 PM.