in reply to catching a pid
Or maybe program_a isn't a pipeline sort of process -- you could try something like the following -- note thatopen( PROC, "| program_a" ); sleep $n; close PROC;
returns the process-id of the command that is launched. It's not pretty, and not portable outside of *n*x, but it worked for me (maybe you need better time resolution on the wait; if so, good luck):open( PROC, "| command" )
If I put parens around the target pid in the "until ()" match, $1 shows up empty outside that block, so I have to do the match again.$spid = open( SH, "| /bin/sh" ); select SH; $|++; print "program_a &\n"; $slept = 0; until ( $ps =~ m{\b$spid\s+$$\s+/bin/sh\b} && $ps =~ m{\b\d+\s+$spid\s+program_a\b} ) { sleep 1; $slept++; $ps = `/bin/ps -o pid -o ppid -o comm`; } if ($ps =~ m{\b(\d+)\s+$spid\s+sleep\s}) { $cpid = $1; sleep ($n-$slept) if ( $n>$slept ); print SH "kill -9 $cpid\n"; } else { print "Damn! Lost the kid.\n"; } close SH;
|
|---|