Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl and pid

Status
Not open for further replies.

abruzzi

Programmer
Mar 27, 2007
17
FR
hello,

I've a script;
in my script a call a unix command,with qx;
how can i do to get the pid of this unix command ?

if i make a "print $$" i've got the pid of the script perl i've run but not the pid of the command;

Can you help me;

thanks
 
maybe you can run the "ps" command and get that information from the operating system, something like:

Code:
open PS, "-|", "ps" or die $!;
print <PS>;
close PS;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
With qx() there isn't a way to get the child's pid directly. You can use a fork/exec combo. Something like:
Code:
if ($child_pid = fork) {
  print "My child is PID: $child_pid\n";
  waitpid($child_pid, 0);
} else {
  exec("my_script");
}
HTH.

Usige
 
my script is like this

Code:
....
foreach my $var(@list_var)
{
my $return = system('./test.pl -i $var &');

}
...

i want to know the pid of the script i run with in system,
to know if this script is end or not;
because the $return variable return 0 instead of my script test.pl is always running;
so i don't know if the script is end good or failed;

???
 
Have you tried,
Code:
my @cmds = `ps -ef | grep 'test.pl'` ;

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Wouldn't you want to change
Code:
foreach my $var(@list_var)
{
my $return = system('./test.pl -i $var &');

}

to
Code:
foreach my $var(@list_var)
{
my $return = system('./test.pl -i $var');

}

That way you get the actual exit code of your script back to $return?

If you leave in the & it will run in the background and you main perl script will just keep on looping because it things the job is done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top