mudMonk has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to catch that pid?
by ikegami (Patriarch) on Jan 20, 2012 at 22:53 UTC | |
|
Re: How to catch that pid?
by JavaFan (Canon) on Jan 20, 2012 at 22:35 UTC | |
|
Re: How to catch that pid?
by BrowserUk (Patriarch) on Jan 20, 2012 at 22:30 UTC | |
|
Re: How to catch that pid?
by Anonymous Monk on Jan 21, 2012 at 04:22 UTC |