in reply to wrong pid

My guess is that the PID you're getting back is the PID of the shell before it forks process_tar.pl. So yes, I think the ampersand is your problem.

If I don't use the ampersand, the parent script waits until the command has completed which defeats the purpose of forking it.

It looks as if you want to send input to the subprocess. Do you want to do that and then not have to wait for it to finish? I suggest you fork first, and run the command after.

my $child_pid = fork(); if ( $child_pid ) { # parent goes about its business } elsif ( defined $child_pid ) { # child my $pid = open(PID, "|$cmd") or die "Can't fork: $!"; # blah blah # this hangs until process_tar.pl finishes. close(PID) or die "Can't close: $!"; exit; } else { die "Can't fork: $!"; }

Replies are listed 'Best First'.
Re^2: wrong pid
by js1 (Monk) on Apr 23, 2007 at 14:04 UTC

    Thanks. You're right. I want to send the filename to the subprocess process_tar.pl.

    #!/usr/bin/perl print "\n" . $ARGV[0]; sleep 40;

    Then after launching this, I want the parent script to carry on running and launch 5 more process_tar.pl's, then wait until one or more of these have finished before running any more.

    So you're saying use fork for this?

      Yes, I was saying use fork for this. Given that you want to launch several processes and wait for them before running more, I might suggest Parallel::ForkManager instead.