in reply to How to monitor a child process from a perl script

How do I get the process ID of it at the first place?
Use the return value of fork, and exec PMEMD in the child. system will only return after the process it spawn has finished.
I know how to open a file and read it and analyze the output but how does the script know when the output file gets updated?
You tell us. Unless PMEMD cooperates in some way, there's no way of knowing. Oh, you can inspect the last modification time of the file and/or its size and/or a checksum, but that's neither foolproof, nor can you know whether PMEMD is done writing, or whether it was PMEMD writing to the file in the first place. And PMEMD may buffer output. Without knowing how PMEMD operates, the question cannot be answered in a meaningful way. Perhaps just doing a tail -f of the file works. Perhaps you need to communicate with PMEMD using a shared memory semaphore.
And how to kill the child process when certain criterion is satisfied?
I guess kill() is a way too obvious way. Perhaps you should disconnect the power? Smash the CPU with a hammer?
  • Comment on Re: How to monitor a child process from a perl script

Replies are listed 'Best First'.
Re^2: How to monitor a child process from a perl script
by oscarjiao (Novice) on Aug 13, 2009 at 22:14 UTC
    Man, you are funny!

    So can I do this:
    my $pid; if($pid=fork()) { open MDOUT "ele.mdout"; #some analysis about ele.mdout #.... #...... if(some criterion here) { kill $pid; } } else { exec("pmemd -i mdin -c inpcrd -r restrt -o ele.mdout"); }
      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.
        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";