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

How do I get the process ID from a system("x &")call? (Context: I need to launch n applications to stress test a server. I want to launch the proceses, wait for them to all die, then I want to cp all of the results in to an appropriate log/run directory, and repeat.)

----
Zak
"There is no room in this country for hyphenated Americanism" ~ Theodore Roosevelt (1915)

Replies are listed 'Best First'.
•Re: How do I get the process ID from a system("x &") call?
by merlyn (Sage) on Sep 19, 2002 at 16:06 UTC
    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

Re: How do I get the process ID from a system("x &") call?
by dws (Chancellor) on Sep 19, 2002 at 17:18 UTC
    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.

      On *nix systems, when you call system() with a single argument, something magic happens under the covers.
      It happens on any system, actually, so long as we're talking about Perl scripts.

      Makeshifts last the longest.

Re: How do I get the process ID from a system("x &") call?
by zakzebrowski (Curate) on Sep 19, 2002 at 16:38 UTC
    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)
      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)
Re: How do I get the process ID from a system("x &") call?
by fglock (Vicar) on Sep 19, 2002 at 16:31 UTC

     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.