Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Determining a Unix PID

by Anonymous Monk
on Jul 25, 2003 at 13:59 UTC ( [id://277860]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easier way to see if a particular program is running with perl rather then using:
ps -ef | grep 'foo_program'
Thanks

Replies are listed 'Best First'.
Re: Determining a Unix PID
by rob_au (Abbot) on Jul 25, 2003 at 14:05 UTC
    You will want to have a look at the Proc::ProcessTable module which I have written about on this site previously here. The following code uses this module to retrieve the process IDs of any processes matching the regular expression passed to the subroutine get_pid:

    use Proc::ProcessTable; my @pids = get_pid( 'httpd' ); sub get_pid { my ( $process ) = @_; my @pids = (); my $proc = Proc::ProcessTable->new; foreach ( @{ $proc->table } ) { if ($_->cmndline =~ /$process/) { push @pids, $_->pid; } } return @pids; }

     

    perl -le 'print+unpack"N",pack"B32","00000000000000000000001001110110"'

Re: Determining a Unix PID
by phydeauxarff (Priest) on Jul 25, 2003 at 14:05 UTC
Re: Determining a Unix PID
by BrowserUk (Patriarch) on Jul 25, 2003 at 14:51 UTC

    If you only need to make the determination once, then the answers above are good.

    If you need to repeat the test, for instance, if you need to keep testing until the process has terminated, then you can save repeating the search process (by whatever method) more than once by retaining the PID from the first search and then use the trick described in perlfunc:kill.

    If SIGNAL is zero, no signal is sent to the process. This is a useful way to check that the process is alive and hasn't changed its UID. See the perlport manpage for notes on the portability of this construct.

    Ie. Once you know the process id

    if( kill 0, $pid ) { # the process is still running } else { # has gone away. }

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      The Camel Book, edition 3, page 735: "A SIGNAL of 0 tests whether a process is still alive and that you still have permission to signal it. No signal is sent. This way you can check whether the process is still alive and hasn't changed it's UID."

      If you want to lose the assumption about UID, but add the assumption that you're working on a system with a proc file system, you could instead do the following...

      $path = "/proc/$pid"; print "process $pid ", -e $path ? "is " : "is not ", "running\n"

      This may or may not be faster, depending on how much "system-callage" the various tests engender, and the relevance of that depends on whether this is a monitoring daemon firing off all fast and furious, or if it's a command line executed script that just does the test once.

Re: Determining a Unix PID
by TomDLux (Vicar) on Jul 25, 2003 at 14:22 UTC

    The easiest way, if it is a program of yours, is to save the PID in a certain location, and delete the file in an END block.

    But the way you say, "running with Perl" makes it sound as if it might be running with perl, or running some other way.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://277860]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found