Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to get the PID of an invoked process

by Anonymous Monk
on Jul 21, 2001 at 01:21 UTC ( #98541=perlquestion: print w/replies, xml ) Need Help??

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

From inside my perl script I want to invoke an external program using system("blah"); is there a way to get the PID of the invoked process without resorting to `ps -ef` and then parsing this? Thanks in advance.
  • Comment on How to get the PID of an invoked process

Replies are listed 'Best First'.
Re: How to get the PID of an invoked process
by LD2 (Curate) on Jul 21, 2001 at 01:31 UTC
Re: How to get the PID of an invoked process
by IraTarball (Monk) on Jul 21, 2001 at 01:43 UTC
    If you need the PID you might want to look at fork and exec. fork will return the childs pid, you can then have the child exec the system call. As I understand it, this is what system() does at it's core. Check out perldoc -f fork and perldoc -f exec for examples and details. I wrote the following that seems to work, no error checking though.
    use strict; use warnings; $SIG{CHLD} = '"IGNORE"'; my $child = fork(); if ($child) { print "I'm the parent, I think I'll wait for the child $child...\n +"; waitpid($child, 0); } else { exec ("ls"); }

    Good Luck,
    Ira.

    "So... What do all these little arrows mean?"
    ~unknown

(redmist) Re: How to get the PID of an invoked process
by redmist (Deacon) on Jul 21, 2001 at 01:33 UTC

    From perldoc perlvar.

    $PROCESS_ID $PID $$ The process number of the Perl running this script. You should conside +r this variable read-only, although it will be altered across fork() +calls. (Mnemonic: same as shells.)

    redmist
    Silicon Cowboy
Re: How to get the PID of an invoked process
by Anonymous Monk on Jul 21, 2001 at 02:15 UTC
    Thanks to all! I'm happily fork'ing/exec'ing away now...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2023-02-03 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (28 votes). Check out past polls.

    Notices?