Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

how can a perl script determine it's own PID while running

by Anonymous Monk
on Aug 14, 2006 at 19:22 UTC ( #567304=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

On Linux OS, how can a running Perl Script determine it's own PID and store it to a variable???
  • Comment on how can a perl script determine it's own PID while running

Replies are listed 'Best First'.
Re: how can a perl script determine it's own PID while running
by Joost (Canon) on Aug 14, 2006 at 19:24 UTC
      OK - this is perfect (exactly what I was looking for)
      Now.... I would like to take this a step further:
      I would like to take the newly found value from $$ and send it to another script, (the sending part is not a problem), and have that new program check on a regular basis (still linux OS) and see if the PID still exists... once the PID that the new program is monitoring finishes the new program will do something.
      The question is: How do I monitor said PID from the current script??
      Thanks Again for Your Great Wisdom!
        You can use the unfortunately named kill function with a signal value of zero to see if a process is still alive.

        my $pid = 1234; if( kill 0, $pid ) { # it's alive } else { # it's dead }
        i'd suggest making a /var/run/ file that containts the PID of your script, like so many other UNIX programs have. Have the file deleted when your script exits. then have your other script monitor that file.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://567304]
Approved by jdporter
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2018-01-23 08:14 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (241 votes). Check out past polls.