www.webdeveloper.com
Results 1 to 6 of 6

Thread: Use Perl to see if a process is running?

  1. #1
    Join Date
    Jan 2008
    Posts
    54

    Use Perl to see if a process is running?

    We're looking at ways to recover from server outages etc so we were looking at process IDs as a possibility. For example, we'd run a Perl script and get the process ID then store that in the database with the job.

    What I'm not sure of is if Perl can be used to check if a process is running. For example, if I use $$ or $PID to get the process ID for a script and save it to the database.

    I'd fire up a script later and see that the current row is being handled by process XXXX but I'd like to know if this process is still running. Is it possible with Perl to take the stored PID and see if it is still running?

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Since you know about PIDs I'm going to guess you are running on a *nix enviroment, right?

    In which case can't you do:
    PHP Code:
    $running = `ps -a | grep {$PID}` 
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Jan 2008
    Posts
    54
    Yeah I tried this but $running always comes up empty.

    Code:
    $check = $$;
    print "This script is $$\n";
    
    $running = `ps -a | grep {$check}`;
    
    print "Running: $running\n\n";
    $$ is grabbing the process ID properly.

  4. #4
    Join Date
    Jan 2008
    Posts
    54
    Using this:

    Code:
    ps auwx
    Pulls information.

    Code:
    root      7770  0.0  0.1   6648  2672 pts/4    S+   10:37   0:00 /usr/bin/perl ./pidtest.cgi
    root      7771  0.0  0.0   4476   964 pts/4    S+   10:37   0:00 sh -c ps auwx | grep 7770
    root      7773  0.0  0.0   3912   672 pts/4    S+   10:37   0:00 grep 7770
    Running it on a non existant pid returns

    Code:
    root      8062  0.0  0.0   4476   964 pts/4    S+   10:39   0:00 sh -c ps auwx | grep 1212
    root      8064  0.0  0.0   3912   676 pts/4    S+   10:39   0:00 grep 1212
    Last edited by halldorr; 01-14-2009 at 09:40 AM. Reason: Updated code

  5. #5
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Yeah, entirely my fault, `` is evaluated before variables, doesn't make sense to me, but meh.
    Code:
    $check = $$;
    print "This script is '$$'\n";
    $running = `ps -A`;
    if( $running =~ /^{$check}\s/){
      print "It's running";
    }else{
      print "It's not running";
    }
    print "\n";
    lower case A will work if the process is started by the same user on the same thing(hard to explain, but I'll try if need be).
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  6. #6
    Join Date
    Jan 2008
    Posts
    54
    Excellent, thanks !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles