http://qs321.pair.com?node_id=826454


in reply to Killing a shell script from within a perl script

use fork, exec, alarm, and kill ... or maybe check out POE.

IPC::Open2 and IPC::Open3 may help do the job as well.

-Paul

  • Comment on Re: Killing a shell script from within a perl script

Replies are listed 'Best First'.
Re^2: Killing a shell script from within a perl script
by strider88 (Initiate) on Mar 03, 2010 at 17:12 UTC

    I tried doing this:

    eval { local $SIG{ALRM}=sub{die "alarm\n"}; alarm 600; $shell_out = system("./sh1.sh"); alarm 0; }; if($@) { die unless $@ eq "alarm\n"; }
    It turns out that the shell script is not even executed.

      The problem with this code is that the shell script isn't being killed.

      alarm just arranges for a signal to be delivered to the Perl process, upon which the signal handler throws an exception (die "alarm\n") which in turn causes the Perl script to continue after the eval {} block. The shell script doesn't know anything of this and happily keeps running.

      It turns out that the shell script is not even executed.

      This is most likely an unrelated problem. Makes me wonder how/why it did run before with the identical system command...

        I too am wondering the same. Meanwhile, I tried executing the following line of code on shell, and it worked:

        perl -e 'alarm shift @ARGV; exec @ARGV' 100 ./sh1.sh
        But I am not able to run this command from my perl script using the system function, within the while loop. That is weird! Any idea why this would happen? Also, the above command does not kill the process, it just suspends it. Is there a way to kill it?