Need help with a script


Results 1 to 6 of 6

Thread: Need help with a script

  1. #1
    Join Date
    Jul 2002
    Location
    TN
    Posts
    1,009

    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

  2. #2
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,947
    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.

  3. #3
    Join Date
    Oct 2003
    Location
    Vancouver, BC, Canada
    Posts
    215
    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

  4. #4
    Join Date
    Jan 2004
    Location
    Ukraine
    Posts
    228
    #!/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

  5. #5
    Join Date
    Mar 2003
    Location
    Earth [I think...]
    Posts
    414
    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.
    *************************************************

  6. #6
    Join Date
    Jul 2002
    Location
    TN
    Posts
    1,009
    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
  •