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

Getting PID of process forked by backticks

by paulski (Beadle)
on Jun 08, 2005 at 00:16 UTC ( [id://464503]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm wondering if it's possible to get the PID of a process forked when you run a system command via backticks. e.g.

#!/usr/bin/perl my $result = `ls -l`;

Is it possible to get the pid of the forked ls command?

Thanks

Paul

Replies are listed 'Best First'.
Re: Getting PID of process forked by backticks
by BrowserUk (Patriarch) on Jun 08, 2005 at 00:28 UTC

    No, but you can get the same effect by:

    my $pid = open LS, 'ls -l |' or die $!; my $result = do{ local $/; <LS> };

    Of course, once you read the input, $pid will no longer be valid as ls will have terminated. But then, the same would be true for backticks.

    All of which leads to the question, why do you want the pid?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Getting PID of process forked by backticks
by merlyn (Sage) on Jun 08, 2005 at 00:36 UTC
    Why do you want the PID of a process that has already exited? Another process may have that PID now. The number is unique and only while the process is running!

    Or are you under the impression that you can run Perl code in parallel with the backquotes?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      What happens if the forked process blocks?

      From PERL's point of view, backticks block until the process returns. But from the OS point of view processes are running concurrently.

      If I know the PID (returned by open), I can kill the process after a certain amount of time has elasped by catching an ALRM signal. The signal will interrupt the control flow and the signal handler can kill the "timed out" process.

        If that's what you want, I'd recommend using fork. I did something similar a while back, using fork, as described in Timing and timing out Unix commands. If you get it working without using fork, I'd be interested in seeing your code.

Re: Getting PID of process forked by backticks
by tlm (Prior) on Jun 08, 2005 at 00:30 UTC

    If there is a way, I do not know it, but you can use open to the same effect; e.g. (adapted from perlfaq8):

    my $pid = open( GREP, "-|", 'grep', @opts, $search_string, @filenames +); chomp(@ok = <GREP>); close GREP;

    the lowliest monk

      Thanks mate, this is exactly what I need. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found