-
Need help with a script
I know absolutely zip about programing but I need something that will listen for when a program quits and then restart it. I have a server running a scorched3d mod 24/7 and a couple of times it dropped and I need it to be restarted automatically.
Any and All help apreciated
thanks
Linux reg. User # 298337
-
Hmm... quick and dirty solution:
Code:
$!/bin/bash
while /bin/sleep 20 ; do
if ! ps -C processname >/dev/null 2>&1 ; then
processname &
fi
done
Change both "processname"s to be the name of the executable (use the full path on the second one, if you want). "ps -C blah" checks whether "blah" is running. If it is, it exits successfully (after printing some unused pieces of information, which we redirect to /dev/null), and if it's not, it exits with failure. The "if" checks the status of ps, and runs "processname" in the background if the ps failed. This is the restart part of the script.
Before checking the status, it sleeps for 20 seconds. This is to prevent it from burning all the CPU cycles it can. The sleep time (20) can be adjusted based on the maximum amount of time you want to wait before restarting it, but setting the time too low will cause a lot of unneeded CPU activity.
To kill the monitoring script, kill the corresponding sleep process. This will cause the while loop to exit, because sleep will not have exited successfully.
Dump this into a text file, and chmod +x it. Then run it in the background, as whichever user is supposed to start your server program.
-
The easiest way to do it would probably be to write a script that grep's the output of the ps command, then restarts the program if it's not listed. There are likely better ways to do it though. Something like (this isn't real bash, but you get the idea):
Code:
while something; do
sleep 10
ps aux > /tmp/progcheck # so we don't grep our grep command
grep -q servername /tmp/progcheck
if $? = 1 restartserver #grep returns 0 if pattern found, 1 if not
done
rm /tmp/progcheck
edit: never mind, use bwkaz's, it's cleaner, and it works
Last edited by AdamZ; 03-31-2005 at 08:17 PM.
Adam
Dell Inspiron 600m
P-M 1.6 GHz | 512MB RAM | IPW2100 Wireless | CDRW/DVD-ROM | 40GB HDD
-
#!/bin/bash
while : ; do
# sleep 1 # you can avoid sleep altogether
if ! ps -C xedit >/dev/null 2>&1; then
xedit
fi
done
just some cosmetic changes. (Don't know either if it's a perfect solution.)
Last edited by nabis; 03-31-2005 at 08:47 PM.
Slackware + *BSD :: RLU 301327
-
How about just putting the following entry in /etc/inittab:
Code:
program_name:2345:respawn:/path/to/program
This will restart the program incase it dies. Check http://publib16.boulder.ibm.com/pser...es/inittab.htm for more details on inittab.
Hope this helps.
- Suramya
--------------------------------------------------
My Website: http://www.suramya.com
My Blog: http://www.suramya.com/blog
Registered Linux User #: 309391
--------------------------------------------------
*************************************************
Disclaimer:
Any errors in spelling, tact, or fact are transmission errors.
*************************************************
-
Thanks all. I'm investigating all above posts.
This place is great.
Linux reg. User # 298337
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|