Results 1 to 10 of 13
Hello,
Im trying to write a script for httpd for a webserver that if it were to go down it would automatically come back...i had a freind help me with ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 12-23-2002 #1Just Joined!
- Join Date
- Dec 2002
- Posts
- 1
Crontab
Hello,
Im trying to write a script for httpd for a webserver that if it were to go down it would automatically come back...i had a freind help me with the script also...and well im wanting to learn some other t hings on my own here is the script....
while [ 1 ];
do HTTP=`ps auwxf | grep httpd | grep -v grep | wc -l`;
if [ $[HTTP] -lt 10 ];
then killall -9 httpd;
sleep 1;
/var/lib/autoadmin/apache/bin/httpd;
fi;
sleep 10;done
what i would like to know does anyone know where ic an find maybe how to implement this into a crontab i never made a cron before...any website would help Thanks i have looked into man cron and other manuals but i think a website would be better thanks!
Jason
- 12-23-2002 #2Linux User
- Join Date
- Jul 2002
- Location
- Daytona Beach, FL
- Posts
- 487
crontabs are wonderfull and easy, check it out
There is my /etc/crontab file, take the setiman script for example this says:SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
#setiman script
00 04 * * * root /seti/server/setiman
MINUTE(0-59) HOUR(0-23) DAYOFMONTH(1-31) MONTHOFYEAR(1-12) DAYOFWEEK(0-6) Note 0 = Sun
so in other words, on the 0th minute if the 4th hour on (classic expansion here) ANY day, ANY month ANY day of the week run the script /seti/server/setiman
Now if you usr redhat they made some easy thing for people to, you have
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
and if you put a script in those folders it will be run respectively at the time, every day, etc... You will also need to put your script in a file, and make sure it has executable permission
save that to some file, and chmod 700 it (you may want root to own it that way noone else can modify it and have your http server shutdown)#!/bin/bash
#this means loop forever
while [ 1 ];
do HTTP=`ps auwxf | grep httpd | grep -v grep | wc -l`;
#the above line counts the number of httpd processes found running
#and the following line says if there were less then 10 found running
if [ $[HTTP] -lt 10 ];
then killall -9 httpd;
#inside the if now, so there are less then 10, kill them all and wait 1 second
sleep 1;
#start apache
/var/lib/autoadmin/apache/bin/httpd;
fi;
#all done, sleep for ten seconds before we loop again
sleep 10;done
Now if you add THAT file to a crontab you will have the first time it is called looping forever, and then teh next call will loop forever .. etc etc so if you want to make it a crontab just do this
Nw you could do this alot of other ways, many of which are cleaner - but congrats on getting the script going in the first place.#!/bin/bash
HTTP=`ps auwxf | grep httpd | grep -v grep | wc -l`;
#the above line counts the number of httpd processes found running
#and the following line says if there were less then 10 found running
if [ $[HTTP] -lt 10 ]; then
killall -9 httpd;
#inside the if now, so there are less then 10, kill them all and wait 1 second
sleep 1;
#start apache
/var/lib/autoadmin/apache/bin/httpd;
fi;
exit 0majorwoo
Quiet brain, or I\'ll stab you with a Q-tip.
- 12-23-2002 #3Linux User
- Join Date
- Jul 2002
- Location
- Daytona Beach, FL
- Posts
- 487
I miss my edit button where i can fix my own typo's
majorwoo
Quiet brain, or I\'ll stab you with a Q-tip.
- 12-23-2002 #4Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Although it admittedly isn't very clean, you could always consider httpd to inittab and make init respawn it for you. If you want to do that, see inittab(5) for more info on that.
Another option is to write a wrapper script for httpd. The advantage of that is that is that httpd will be restarted immediately, not just when the script is run by cron. Here follows an example of such a script (I'm just writing it down off the top of my head, so it may very well contain some typos).
The first line in the while loop specifies is that it starts httpd, but doesn't wait for it to exit. The second line might be a little harder. The "$!" is a built-in variable that always contains the PID of the last process started in the background, in this case httpd. The wait command just waits until httpd exits. If there were just these three lines in the loop, httpd would have to be exited by first killing the script and then killing httpd itself. If that's ok for you, you don't need the "trap" commands. The first of these commands makes the shell run "kill $httpd" (i.e. killing the PID it got the last time it started httpd) when it exits. The second makes sure it exits when it receives SIGTERM instead of having the kernel eliminate it, in which case it could never get to run "kill $httpd" before it exits. This way, you can kill the script, and have the script kill httpd for you just before it exits, itself. If you just kill httpd, the script will, of course, restart it.Code:#!/bin/sh trap 'kill $httpd' EXIT trap 'exit 0' SIGTERM while true; do /var/lib/autoadmin/apache/bin/httpd & httpd=$! wait $httpd done
If you want to use crontab, however, just do as majorwoo tells you.
Btw, have you looked at the crontab(5) manpage? I think it tells about everything.
One more thing, though. That ps command you use in your script seems to have an unneccesary number of options. Doing "ps -Aw" would function just as good.
- 12-25-2002 #5Linux User
- Join Date
- Jul 2002
- Location
- Daytona Beach, FL
- Posts
- 487
something ive alwyas wodered...
if the man page for ps says -A Selects ALL processes
why does anyone use any options besides -A when they want to check all processes?majorwoo
Quiet brain, or I\'ll stab you with a Q-tip.
- 12-25-2002 #6Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
I, too, have failed to see that.
- 12-25-2002 #7Linux User
- Join Date
- Jul 2002
- Location
- Daytona Beach, FL
- Posts
- 487
i mean its not microsoft code in which case you would have to know that
-A only selects all processes that Bill thinks you should be seeing, i mean after all you don't need to know that their auto update client is currently sucking up all your bandwidth downloading their 20MB .net framework do you?majorwoo
Quiet brain, or I\'ll stab you with a Q-tip.
- 12-25-2002 #8Linux User
- Join Date
- Jul 2002
- Location
- Daytona Beach, FL
- Posts
- 487
</cheap M$ bashing>
majorwoo
Quiet brain, or I\'ll stab you with a Q-tip.
- 12-25-2002 #9Linux Enthusiast
- Join Date
- Jun 2002
- Location
- San Antonio
- Posts
- 621
I use `ps aux` to see all processes and which users are running them. I use auxww if the commands are long and run off the end of the screen. 99% of the time my `ps` commands are passed to grep, just to see if something in particular is running.
I respectfully decline the invitation to join your delusion.
- 12-25-2002 #10Linux User
- Join Date
- Jul 2002
- Location
- Daytona Beach, FL
- Posts
- 487
ditto on the grep...
majorwoo
Quiet brain, or I\'ll stab you with a Q-tip.


Reply With Quote
