how to kill a spawned process when it hangs

dn_perl_at_hotmail.com
Date: 05/04/05


Date: 4 May 2005 03:10:50 -0700


I run a perl script in which I call an interactive process which is
automated via a HERE document. But if something goes wrong with the
called process, say password has changed and my perl script is passing
the old, now-wrong password, it gets stuck and sort-of hangs. I would
like my code to address this situation. Smooth execution of the process
normally takes less than a second, at worst it could take 15 seconds. I
would like to add a while loop which keeps checking after a two-second
pause whether the called process has terminated. If it is still running
after 15 seconds, I would like to kill it.

Sample HERE-document (a shell-script named : exec_this.tmp) contents :
call_app << TO_HERE
username
password
option_number
more_inputs
TO_HERE

Perl Code (untested, to give the reader an idea of the flow) :

#!/bin/perl
use strict ;
`my_dir/exec_this.tmp` ; # (I may want to capture the pid of the
exec_this.tmp proc
    # started via these back-tics. There are other ways to guess the
pid with
    # reasonable accuracy, but capturing it would be fool-proof.)
# How to capture pid of the process started from within those
back-ticks?
# Please advise.
my $time_count = 0;
while (1) {
    sleep 2 ;
    $time_count = $time_count + 2;
    if PID/PROC combo no longer active { last ; } ;
    if( $time_count > 15 ) {
        ` kill -9 PID ` ;
        last ;
    }
}

How to read PID of the process started from within the backticks,
though?
Please help. Thanks in advance.



Relevant Pages

  • Re: Catch pid for a process that created
    ... > Mav wrote: ... >> I am writing a perl script on windows that try to get the pid for a ... >> get that pid. ...
    (comp.lang.perl)
  • Re: How to capture pid
    ... you are getting the parent pid in the perl script. ... you are doing an exec. ...
    (perl.beginners)
  • Re: PID of Service
    ... Given the name of a Win32 service, is there a way to find out its PID from a ... Perl script? ... Ing Ferry Bolhar ...
    (comp.lang.perl.misc)
  • VBA Shell function doesnt work
    ... The Shell in VBA in Office X always returns PID 0 and doesn't execute ... I want to run a perl script. ... code works on Windows XP. ... VBA expects Macintosh-style paths using colons, in my case, something ...
    (microsoft.public.mac.office.excel)