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

I'm trying to write a perl script that forks off another program and monitors its execution during run time. Something like the following snippet:
if ( ! defined($kidpid = fork() ) ) { # fork returned undef, so failed die "Cannot fork: $!"; } elsif ($pid == 0) { # fork returned 0, so this branch is child # want to tun program like this: ( time exe < stdin > stdout + ) >&! stdlog exec( code to run program goes here.... ); # if exec fails, fall through to the next statement die "can't exec date: $!"; } else { # fork returned 0 nor undef so this branch is parent # run-time monitoring code goes here.... waitpid($kidpid, 0); }
I want to use a while(1) loop and wait for the child's pid, so I know when the executable's completed. Any idea on how to do this? I'm able to run the executable in background (using a system call) but w/o run-time monitoring (don't know child pid)? Thanks. E.

Replies are listed 'Best First'.
Re: forked subprocess run-time monitoring
by belg4mit (Prior) on Apr 16, 2002 at 23:15 UTC
    I would suggest you look at perlipc. If you use certain forms of open, you can accomplish one or two of your tasks (fork or fork and exec), as well as get the pid of the child back. This might make monitoring easier as well, depending on how you are going about that (checking the stdout), if not then consider opening and dupping a filehandle for STDIN before you open your child.