Restarting a Crashed Process

Hello,
I host a couple of Call of Duty gameing servers. There are some hackers who love the crash them. When they crash them it simply causes a segmentaion fault and kills the PID. I was wondering it you could help me write a script to simply restart the program after it has been crashed. The program works as follows: If you want to start a server you simply type: "./cod_lnxded +exec p31.cfg &" It then creates a backgrounded PID. I have created a script to launch about 10 of these. Now all I want is a script to see if any of these crases and to restart them.

Thanks a million,
Robert

use ps -ef | grep "procesname" | grep -v "grep"

if you do n't get any o/p ... start that process in the script.

put this in a loop which checks process status for certain time period.

or setup the cron job.

Before that ...

just have a thought about hackers to your system

yeah...... It would be great if I knew how to do that.

I knew the method I just don't know how to shell script, heh.

can you post your start script?

#!/bin/bash

./cod_lnxded +exec sam.cfg &
./cod_lnxded +exec h20war.cfg &
./cod_lnxded +exec cypher.cfg &
./cod_lnxded +exec ryan.cfg &
./cod_lnxded +exec cell.cfg &
./cod_lnxded +exec alls.cfg &
./cod_lnxded +exec p31.cfg &
exit 0

Why did you choose to exit your script? I probably would try something like this:

For each process
Check to see if a process is running based upon your script name
if no process is found
Submit the process in the background
Create a lock file containing the PID value
Determine an appropriate wait interval then poll each process based on the
value contained in the lock file. If the process is no longer running, restart it, otherwise check the next process.

Run the script from a crontab but make sure you only have 1 instance running at a time (again use a lock file for the main script).

you could also ...

  1. count the # of processes running and do nothing if all 10 are there
  2. if less than 10 processes, identify which ones are not running and restart them

... if you have the monitor script sleeping and checking every second, your downtime per process will be quiet short ...

however, you might want to fix that hacker issue as soon as possible as anything you can do to fix your process --- they can break ...

Well the hacker thing isn't that big of a deal. When you are playing the game a player can type a certain command which will cause the server to have a segmentation fault and crash - a buffer overflow I believe. What I was thinking of doing was a ps for each process with the certain conf file. Then put that in a loop and if it doesn't exist then restart the process. However, I don't see how I can output the contents of ps so that i can check this. Like how do you do this: ps -arguments < temp.file

Link that shows one method of creating a lock file. Also shows an example of storing, retrieving, and checking the process based on the PID

i'd try counting the number of processes first before i'd check for which process-configuration pair needs to be restarted ... here's my version of events ... put in cron to run every minute or hack what you need off it and put in your script in perpetual loop ...

#! /bin/ksh
numproc=7
a="sam.cfg h20war.cfg cypher.cfg ryan.cfg cell.cfg alls.cfg p31.cfg"
if [ `ps -ef | grep -wc "cod_lnxded"` -lt "$numproc" ]
then
    ps -ef | awk '/cod_lnxded/ && !/awk/' > /tmp/$$
    if [ -s /tmp/$$ ]
    then
        for cfg in $a
        do
            grep $cfg /tmp/$$ > /dev/null
            if [ $? -ne 0 ]
            then
                  /path/cod_lnxded +exec $cfg &
            fi
        done
    else
        for cfg in $a
        do
            /path/cod_lnxded +exec $cfg &
        done
    fi
    rm /tmp/$$ 2> /dev/null
fi

exit 0