Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to catch that pid?

by mudMonk (Initiate)
on Jan 20, 2012 at 22:20 UTC ( [id://949064]=perlquestion: print w/replies, xml ) Need Help??

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

    My problem: I realized open3 returns the pid only at closure.

    If "at closure" means when the child exits, that's wrong. open3 returns the childs pid, and open3 returns as soon as the child is launched.

    use feature qw( say ); use IPC::Open3 qw( open3 ); open(local *TO_CHLD, '<', '/dev/null') or die $!; my $pid = open3( '<TO_CHLD', local *FR_CHLD, '>STDERR', 'perl', '-E', 'sleep 2; say $$;' ); say time, ": ", $pid; while (<FR_CHLD>) { print time, ": Perl said: $_"; } waitpid($pid, 0);
    1327100100: 27601 1327100102: Perl said: 27601
Re: How to catch that pid?
by JavaFan (Canon) on Jan 20, 2012 at 22:35 UTC
    Well, the usual way is to use fork, which will give you the PID, and then an exec in the child. In fact, assuming a Unix or Unix like platform, this will be happening being the scenes regardless how to start a different process.
Re: How to catch that pid?
by BrowserUk (Patriarch) on Jan 20, 2012 at 22:30 UTC
    My problem: I realized open3 returns the pid only at closure.

    If you add an & to the end of your command, it will be run asynchronously and open3 will be able to return the pid to you immediately.

    You would need to add a shell into the mix though.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: How to catch that pid?
by Anonymous Monk on Jan 21, 2012 at 04:22 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://949064]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found