Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

How to catch that pid?

by mudMonk (Initiate)
on Jan 20, 2012 at 22:20 UTC ( #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 (Pope) 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 (Pope) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://949064]
Approved by Corion
Front-paged by Corion
choroba played with the band on Saturday, so Sunday was very sleepy
[choroba]: managed to release Syntax::Construct with 5.28 support in the night, though
[Corion]: choroba: Whee ;)
[Corion]: marto: Ouch - I would've thought that kids adapt much better, but that's obviously not the case...
[marto]: well, their mother let them sleep till 15:00 & 12:00 last week, which didn't help them adjust :P
[Corion]: I was "productive" over the weekend in the sense that I revived my old "Perlmonks on SQLite" code, which likely means I can get a test instance back up running on my webhost. Small steps :)
[Corion]: marto: Ow, no, that doesn't help at all :)
[choroba]: Related to the new release, anyone could explain this or this tester report?
[Discipulus]: hello crew! marto thanks for the message: but I how can I help? i'm testing cpan Padre atm problem with Client::Debug
[choroba]: I don't happen to have 5.10.0 nor 5.8.5 handy...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2018-06-25 08:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.