My need: I want to run an executable and kill it when I'm done with it. (OS is linux)
My problem: I realized open3 returns the pid only at closure.
My Question: How to run a system process and capture its pid while it's still running?
I've written this mangled piece of code as a patch for now, but I'm sure there are much better answers out there.
Your thoughts?
sub pidKill { #----------------------------------------------------------- # USE: kills the last process seen in output of # ps. # (This begs to kill the wrong program.) # ARGUMENTS: $string containing program, arguments, and possibly a s +udo in front. # RETURN: none #----------------------------------------------------------- my $program = shift; my (@output,$line); if ($program =~ /^sudo /) { $program=$'; } if ($program =~ / /) { $program=$`; } while ($program =~ /\//) { $program=$'; } @output = `ps -eo "%p %c" | grep $program`; foreach (@output) { $line = $_; } trim($line); # like chomp but can strip leading spaces too. $line=~/ /; $line = $`; #should have just the prog now. kill 'KILL' , $line; }
In reply to How to catch that pid? by mudMonk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |