EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I would be running this perl script and inside the script, I do many system commands in which i start running other programs on other servers. If I do ctrl C on my program, it won't stop the programs running on the other servers. I have a system command want to enter to kill the programs before I abruptly exit my perl program using ctrl C. Is there any way to do this?

Replies are listed 'Best First'.
Re: kill command in perl?
by etcshadow (Priest) on Jul 30, 2004 at 03:28 UTC
    If I understand what you're asking, it's as simple as:
    $SIG{INT} = $SIG{TERM} = sub { kill 15 => @process_i_want_to_term; exit; };
    or perhaps even
    END { kill 15 => @process_i_want_to_term; }
    ------------ :Wq Not an editor command: Wq
Re: kill command in perl?
by pbeckingham (Parson) on Jul 30, 2004 at 03:27 UTC

    Take a look at signal handlers and the kill function.

Re: kill command in perl?
by hbo (Monk) on Jul 30, 2004 at 07:20 UTC
    But if those processes are running on "other servers," you will have to issue a remote kill, with the shell, in that signal handler:
    $SIG{INT} = $SIG{TERM} = sub { `ssh remote_server killall -9 everything`; exit; };
    You might want to be more selective than in the example. 8)

    "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers

      The easiest way to be more selective, is to generate a random number, md5 it and pass it along as a dummy parameter. I've done that by wrapping the target server program in a perl script, and simply ignoring it. When I have to kill the thing off, i check the process list for that random number so that I kill less things off. I also have the perl script on the remote server kill itself off in reasonable time in case someone yanks a net cable.

      Bart: God, Schmod. I want my monkey-man.