Re: How to capture pid

From: John McKown (joarmc_at_swbell.net)
Date: 03/03/04


Date: Tue, 2 Mar 2004 17:59:12 -0600 (CST)
To: Perl Beginners Mailing List <beginners@perl.org>

On Tue, 2 Mar 2004, Harry Putnam wrote:

>
> What is the handy way to record the pid of $some_process in this
> fake code?
>
> $some_process = 'tcpdump -v -ieth0 >file')
> system("$some_process") or die "Can't start $some_process: $!";
>
> print "Pid is $pid\n";
>
> The process is started by a more elaborate perl script at bootup.
> and restarted every four hours from syslog. Code above is way simplified.
>
>

I don't think you can get the PID when using the system() function. Also,
I am fairly sure that system() does not return control to your Perl script
until the function it invokes finishes. Would something like:

use Errno qw(EAGIN);
...
$someprocess=...
FORK: {
if ($pid=fork) {
        print "PID is $pid\n";
}
elsif (defined $pid) {
        exec $someprocess;
}
elsif ($!=EAGIN) {
        sleep 5; #wait five seconds
        redo FORK; #then try again
}
else {
        die "Cannot fork to run $someprocess $!";
}
}

--
Maranatha!
John McKown


Relevant Pages

  • Re: How to capture pid
    ... > What is the handy way to record the pid of $some_process in this ... print FILE while; ... close DUMP or die "Cannot close pipe from tcpdump: ...
    (perl.beginners)
  • How to capture pid
    ... What is the handy way to record the pid of $some_process in this ... The process is started by a more elaborate perl script at bootup. ...
    (perl.beginners)
  • Re: Catch pid for a process that created
    ... > Mav wrote: ... >> I am writing a perl script on windows that try to get the pid for a ... >> get that pid. ...
    (comp.lang.perl)
  • how to kill a spawned process when it hangs
    ... I run a perl script in which I call an interactive process which is ... # started via these back-tics. ... # How to capture pid of the process started from within those ...
    (comp.unix.shell)
  • Re: How to capture pid
    ... you are getting the parent pid in the perl script. ... you are doing an exec. ...
    (perl.beginners)