How to get pid, which is started with "system()"?

T

Ting Wang

Is there a way to get the process ID, which is started with system()?
e.g
#! /usr/bin/perl
#test.pl
#platform: linux
system(app);
system(app);

and i want to kill the first "app". With

sub kill_app{
my $app = "\"".$_[0]."\"";
if ($_[1]) {
my $pid = $_[1];
$line = `ps -ef |grep $app|grep $pid|grep -v grep`;
}else
{
$line = `ps -ef |grep $app|grep -v grep`;
}
@ps_info = split (/\s+/, $line);
$pid = $ps_info[1];
if ($pid =~ /[1-9][0-9]+/) {
kill 9, $pid;
}
}

will both app be killed. and "kill_app" has problem when the application
is "grep".

in shell script i can use $! get the pid.
How can i do it with perl?
I really do not want to add a small shell script in perl.

Thanks
 
B

Brian McCauley

Ting Wang said:
Is there a way to get the process ID, which is started with system()?

Not simply and it would not very usefull anyhow as by the time the
system() function returns the process has completed.

On a POSIX system you can decompose system() into fork() and exec() as
in the spawn() function in the CPAN module Run.

It would be nice if there was a module on CPAN implemented a portable
spawn() but my quick search has failed to find one.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top