Cron to Restart Apache

labrocca

Verified User
Joined
Mar 12, 2006
Messages
130
Occassionally my Apache locks up from reaching max connections while under a DDOS attack or other various problems. I created a cron job that checks if Apache is running or if it's processes are high. It then restarts Apache and sends me a notifcation email. Here is the problem. It doesn't work.

This is the script.

Code:
#!/bin/sh
isAlive=`/bin/pgrep httpd | wc -l`
if [ $isAlive -gt 400 ]
then
/usr/local/etc/rc.d/httpd restart
mail -s "Server Restart A" [email protected] < /log/check_mail.txt
fi

if [ $isAlive = 0 ]
then
/bin/sh /usr/local/etc/rc.d/httpd start
mail -s "Server Restart B" [email protected] < /log/check_mail.txt
fi

The script itself works as I get notification emails but for some reason the httpd start/restart doesn't work from cron. It works fine if I ssh in and run it. So what's the problem? Please advise as this is very important for me.

This is my root cron btw so I it's running as root.

I've tried to change ownership of the check script but so far no luck.

Help is greatly appreciated.
 
I have the file (check) as 755. The file/cron runs as I get the notification emails.

If I'm in ssh and I run the file with apache down it will restart it. So I know the command works then.

I'll try doing these:
/usr/local/etc/rc.d/httpd start
/usr/local/bin/bash /usr/local/etc/rc.d/httpd start

Not sure it will do anything different though.
 
Ok i missunderstood your first post.

How do you know that apache is not restarted?

Try to use two line way, stop and then start.

Regards
 
Ok i missunderstood your first post.

How do you know that apache is not restarted?

Try to use two line way, stop and then start.

Regards

I know it doesn't restart because it's not running. What kind of noob you think I am that I don't know apache is running.

I've tried restart and start. I know Apache is stopped because I get the email notification on each area where httpd process is checked. So I know my if statements are working.

I think this boils down to a permission problem. Either check script has to be owned by someone specific to start apache from a cron or the cron has to be run by another user. Not sure the right combo. I was hoping someone else is doing this already and can offer the solution.

I've tried to fix this a few times. I need a 100% working cron job check and restart that works with Directadmin. I can't believe no one runs something like this already. Yet I can't find any existing threads here. One would think a simple cron for Apache Run Check and Restart If Not option would be laying around.
 
wasnt my intention saying your are noob, maybe my english is not enough good for let me explain well.

I mean, if apache is running and the script start, how do you notice that he didnt restart if was running?

Or, if the email you receive is just in case there are problem with apache, have you tryed to use killall -9 httpd and then use the restart command?

the line in /etc/crontab should be something like

Code:
0 * * * * root PATH

or, if you using to add in crontab with command crontab -e you can skip the "root" word

Regards
 
Apache is NOT running after the script runs.

Apache goes dead. The cron script runs. I get notification. Apache still dead.

I have to SSH in and restart apache manually.

I have the script running every 10 mins. So let's say Apache locks up with 500 processes. The script kills them. Apache is now dead. The "restart" doesn't work. Then the "start" doesn't work 10 mins later when cron script runs again. So the script actually works to stop Apache just fine. What doesn't happen is that the process restarts or starts.

I think I'll have to shut my site down. Set cron to run every minute and review what it's doing and try to get it to work. I'll just have to eat the downtime.

Would have been nice to see a response in here from someone using a cron job to check apache and restart it. Amazing no one here does that.
 
I understand, thats why ive asked about how did u set the cron, i thot about permission problem.

What OS are you using? I think FreeBSD for the restart path, am i wrong?

Cause i was also think about tell you to try /etc/init.d/httpd restart but maybe on bsd doesnt exist as valid path.

Sorry if i couldnt be more helpful on this

Regards
 
Yes FreeBSD.

/etc/init.d/httpd

That's not a valid path. I have the httpd rc.d start script path correct.
/usr/local/etc/rc.d/httpd

The thing is that apache seems to stop because of the script. But it doesn't start. Wish I knew why. Cron shows no errors. error_log shows nothing too.
 
Yes that's why I stated "error_log shows nothing too." Those are apache error logs.
 
Well, on the link i posted was just a "exe" path wrong, so, maybe is the same.

Try to set a cron to run each 10/15mins so you can test it now :)

Regards
 
Firstly, If apache is dead, doesn't directadmin try to start it? Directadmin does monitoring apache status, and tries to start it, if it does not work.

If directadmin fails to start your apache, then it sends you a warning by email and in ticket system.

So, you could check if you've got apache/httpd in directadmin on services pages. If no, you might want to re-add it.

Secondly, if apache troubles you with 500 max users, so you might need to lower the value. Additionally, you'd better tune apache, or even add a light-weight frontend to apache, something like lighttpd or nginx, or even oops/squid.
 
Back
Top