Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Backticks and SIGALRM

by jbert (Priest)
on Aug 20, 2007 at 13:40 UTC ( #633824=note: print w/replies, xml ) Need Help??

in reply to Re^2: Backticks and SIGALRM
in thread Backticks and SIGALRM

Ah, OK. So you want to be able to kill off the offending sub-process when the alarm fires? Then you either need to walk the process table, looking for a process whose parent is your script, or start the process in a way which allows you to get the pid and the output.

e.g. run the code under a piped open, something like this (untested):

my $cmd = "your command"; my $pid = open(my $fh, "$cmd|"); $SIG{ALRM} = sub { print "Alarm fired, killing pid $pid\n"; # Replace with windows-specific code to kill process kill 15, $pid; }; die "Can't run command [$cmd] : $!" unless $pid; # Or do some other processing with $fh push @result, $_ while (<$fh>); close $fh or die "Problem running [$cmd] : $?";
If your child process starts yet more children (and they don't exit when the parent is killed), you'll have to write more elaborate cleanup code.

Dig through the windows-related process modules on CPAN and you should find what you need for the cleanup.

Replies are listed 'Best First'.
Re^4: Backticks and SIGALRM
by sgt (Deacon) on Aug 21, 2007 at 14:14 UTC

    Just a minor pick (which applies to all posted solutions so far) , on unixlike the killing part should be in at least two stages: first send signal of normal termination (SIGTERM) and then signal of immediate termination (SIGKILL) to let the ext. app a chance to clean up. In some cases even stop/cont signals can be appropriate. It is probably better to send signal to the process group id (perl kill with negative signal, or system shell kill with negative pid) or to take care of the external app potential children (or simply imagine you fired a shell...); if the external app has acted "daemonic" then one should have knowledge about the app's behaviour, /proc or a process table walker could be of help.

    cheers, --stephan

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://633824]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-05-28 10:43 GMT
Find Nodes?
    Voting Booth?