Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Backticks and SIGALRM

by nemo (Sexton)
on Aug 20, 2007 at 13:30 UTC ( #633819=note: print w/ replies, xml ) Need Help??


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

Hi Jbert,
I haven't tested your code but it will work correctly, I tested the alarm code before with Inifinite loops and it works as expected. The problem is the hanging app in the shell. I am running on MSWIN32-x86-multi-thread Perl version 5.8.8.

Thank you for your replies.
Nemo


Comment on Re^2: Backticks and SIGALRM
Re^3: Backticks and SIGALRM
by jbert (Priest) on Aug 20, 2007 at 13:40 UTC
    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.

      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://633819]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-10-31 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls