in reply to Re^2: How to monitor a child process from a perl script
in thread How to monitor a child process from a perl script

More or less. Good practises demand you test whether $pid is actually defined - if it's undefined, the fork failed. And you ought to check whether exec failed.

Replies are listed 'Best First'.
Re^4: How to monitor a child process from a perl script
by oscarjiao (Novice) on Aug 14, 2009 at 22:04 UTC
    So I tried to print out pid in parent process. It turned out to be 22718. However I did "top" right after I launched the script. The ID of main process is 22717 while that of kid process is 22719. As a result the kid process is not killed. Why is that? My code looks like this:
    if ($pid=fork) { print $pid, "\n"; do{ if (-e "mdinfo") { $last=`tail -1 mdinfo`; if ($last =~ /Density/) { @last_mdinfo=split ' ',$last; $den=$last_mdinfo[2]; } } } while ($den < 1.0000 or $den > 1.005); kill 15, $pid; } elsif (defined($pid)) { exec(qq(/opt/amber/bin/sander -i ele1.0.mdin -c ele1.0.inpcrd -p e +le1.0.prmtop -o ele1.0.mdout -r ele1.0.restrt &)); exit(0); } else { print "child was not created\n"; } print "done\n";
      You're execing a single string which contain spaces and other characters that are special (the &). In that case, Perl execs a shell (with pid 22718) which then calls /opt/amber/bin/sander (with pid 22719).

      Use the list form of exec instead.