Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Getting the pid of the process launched by my perl program

by Anonymous Monk
on Dec 21, 2004 at 12:25 UTC ( [id://416438]=perlquestion: print w/replies, xml ) Need Help??

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

Hi PERL Monks,

I'd like to get the process id of the background process launched from within my perl program. I'm trying to do this in my perl code -

system (./a.out &);
I want know the process id of the newly lauched process "a.out". I can get this in a C shell script -
./a.out & echo $! > /tmp/pidfile
How will I do this in perl ? Also, I don't want to call the C shell script from the perl program.

What do I do ??

thanks in advance !!

Replies are listed 'Best First'.
Re: Getting the pid of the process launched by my perl program
by edan (Curate) on Dec 21, 2004 at 12:40 UTC
    $SIG{CHLD} = 'IGNORE'; # on systems where it's supported defined( my $pid = fork ) or die "fork: $!"; if ( $pid ) { # parent print "child pid is $pid\n"; } else { # child exec("a.out") or die "exec: $!"; }
    --
    edan

      Thanx much edan and ozone !!
      I'm using the same logic to monitor the child process with a timer in the parent. On maximum timeout I'm killing the child process. The issue is I'm running an exe from the child which doesn't die on kill the child. I tried to explicitly kill the process with taskkill on windows and it works fine. But I face portability issues because of using taskkill. Can anybody help on this?
Re: Getting the pid of the process launched by my perl program
by ozone (Friar) on Dec 21, 2004 at 12:37 UTC
    The current process id is stored in $$, but I suspect that what you're trying to do is find out when the background process has died, right? If so, you'd be better off using fork(), exec() and waitpid.

    A quick search produced a helpfull node that could help: ForkMe

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found