WEB script to check if process is alive. if not... start it

Discussion in 'OT Technology' started by Ricky, Feb 27, 2009.

  1. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    for linux.

    anyone mind making a small script for me? :o
     
  2. sonicsuby

    sonicsuby New Member

    Joined:
    Mar 15, 2000
    Messages:
    21,338
    Likes Received:
    0
    Location:
    CA
    For work, school, play, what?
     
  3. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    play

    just for one of my small sites. im sure it's not hard at all. i just suck at teh linux/programming.
     
  4. 2ofdem

    2ofdem OT Supporter

    Joined:
    Jun 17, 2003
    Messages:
    114,854
    Likes Received:
    8
    Location:
    aus.vic.mel
    #!/bin/bash
    output= ps -aux | grep 'Processnamegoeshere' | wc -l
    if [ "output" < 1 ]; then
    command to restart process
    fi

    And just chuck it in your cronjobs
     
    Last edited: Feb 27, 2009
  5. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    ibmywhat
     
  6. 2ofdem

    2ofdem OT Supporter

    Joined:
    Jun 17, 2003
    Messages:
    114,854
    Likes Received:
    8
    Location:
    aus.vic.mel
    Sorry chuck that in an .sh file and run it from cron.
     
  7. 2ofdem

    2ofdem OT Supporter

    Joined:
    Jun 17, 2003
    Messages:
    114,854
    Likes Received:
    8
    Location:
    aus.vic.mel
    And woops its < not >
     
  8. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    thank you kind sir
     
  9. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    so use your edit button
     
  10. sonicsuby

    sonicsuby New Member

    Joined:
    Mar 15, 2000
    Messages:
    21,338
    Likes Received:
    0
    Location:
    CA
    This will do it in Perl.

    Code:
    #!/usr/bin/perl
    
    ###
    # This script will check for the existence of a running
    # process, report its status and attempt a single restart
    # if it is down.
    #
    # $ps = path to the ps command
    # $grep = path to the grep command
    # $processName = the process to check
    # $processCount = the amount of processes that should be running
    # $processStart = path to the start script for the process
    #
    # This is intended to be used for single process name; however,
    # it could be easily adapted to look for multiple processes by
    # changing the top variables to hashes.
    #
    # I've set $SIG{CHLD} to ignore.  Other than that,
    # I'm not doing anything special to make sure we wait for our
    # forked children to go away, so if that logic isn't added, you
    # could end up with a bunch of zombie processes.
    ####
    
    $ps = "/bin/ps";
    $grep = "/bin/grep";
    $processName = "processname";
    $processCount = 1;
    $processStart = "/path/to/start/script";
    
    $SIG{CHLD}='IGNORE';
    
    if (&checkProc($processName,$processCount,$processStart))
    {
            print "Process $processName is down.\n";
            $rc = fork();
            if ($rc == 0)
            {
                    exec(`$processStart`);
                    exit 0;
            }
            if (&checkProc($processName,$processCount,$processStart))
            {
                    print "Process $processName still down.  Exiting.\n";
                    exit 0;
            }
            else { print "Process $processName restarted!\n"; exit 0; }
    }
    else { print "Process $processName is up.\n"; exit 0; }
    
    sub checkProc
    {
            my ($procName,$procCount,$procStart) = (@_);
    
            if (`$ps -ef | $grep -v grep | $grep -v \"$procStart\" | $grep -c \"$procName\"` < $procCount)
            {
                    return 1;
            }
            else { return 0; }
    }
     

Share This Page