You don't, directly.
Consider what you've done. You've asked a shell to fork a process, and then exit itself. The shell certainly knew the child's PID, but you didn't ask the shell to tell you.
So, you'll either have to scruff through the process table yourself (hard), ask the shell to tell you before it exits (easier), or simply do the forking yourself (easiest, and most efficient).
Read up on fork for details. I'm sure I have a column or two on that as well.
-- Randal L. Schwartz, Perl hacker | [reply] |
How do I get the process ID from a system("x &") call?
zakzebrowski gives a partial answer above, and a bit more background might help you understand how to take it from there.
On *nix systems, when you call system() with a single argument, something magic happens under the covers. To handle shell meta-characters, system() invokes a shell, and the shell processes the argument. This muddies the waters. You clearly don't want the shell's process id, but it's gotten in the way. To get the process id of x, you need a finer grain of control, which can be had by an explicit fork()/exec().
After a fork() the parent process gets the process id of the child. The exec() replaces the child copy of the parent process, reusing the process id. There are a couple of things you need to watch out for. One is that if the exec() fails, the parent still thinks it has a valid process id. You can communicate this back to your parent process by a number of means, including an exit() code.
| [reply] [d/l] [select] |
| [reply] |
This code works nicely... :)
print "About to fork:\n";
my $pid = fork();
if ($pid ==0)
{
exec "sleep 5";
exit(0);
}
else
{
print "Child PID: $pid\n";
}
print "Parent continuing...\n";
----
Zak
"There is no room in this country for hyphenated Americanism" ~ Theodore Roosevelt (1915) | [reply] [d/l] |
Update -- zombie processes never die... See fork man page for details...
my $pid = fork();
if ($pid ==0)
{
unless(fork){
print "Child launched!\n";
exec"/bin/sleep 10"; # server process
exit 0;
}
exit 0;
}
waitpid($pid,0);
$pid=$pid+1; // cheating process ids are 1+ what we think they a
+re
print "Child PID: $pid\n";
push @pidArray, $pid;
}
Thanks guys!
----
Zak
"There is no room in this country for hyphenated Americanism" ~ Theodore Roosevelt (1915) | [reply] [d/l] |
bash allows this:
perl test.pl & jobs -nl > out.txt
out.txt will contain a line like:
[2]+ 27872 Running perl test.pl &
You can call it from Perl using (untested) (see update):
open (FILE, "perl test.pl & jobs -nl |");
update: I had to use this in order to make it work:
open (FILE, "perl test.pl & jobs -nl | cat |");
update: don't use this. It is not portable. Use fork instead - see below.
| [reply] [d/l] [select] |