in reply to Killing of ALL children made by Open3

Update: I just re-read your post, and I didn't answer your question at all! Sorry; I misunderstood what you were asking. I'll give it another whack in a seperate post.

IPC::Open3 returns the PID of the process it creates, so you can simply remember that and kill it later. For example:

#!/usr/bin/perl -w use strict; use IPC::Open3; { my %pidlist; sub my_open3 { my $pid=open3(@_); $pidlist{$pid}=1 if $pid; warn "my_open3: Started process $pid\n"; $pid; } sub my_killkids { my($signal) = shift; warn "my_killkids: Killing processes ",join(" ",keys %pidlist),"\n +"; kill $signal, keys %pidlist; } sub my_chld { my $pid = wait; warn "my_chld: Child $pid finished.\n"; delete $pidlist{$pid}; } } $SIG{CHLD} = \&my_chld; my_open3(\*STDOUT,\*STDIN,\*STDOUT,"sleep 10"); my_open3(\*STDOUT,\*STDIN,\*STDOUT,"sleep 10"); my_open3(\*STDOUT,\*STDIN,\*STDOUT,"sleep 10"); my_killkids('INT'); sleep(1); sleep(1); sleep(1); my_killkids('KILL');

Replies are listed 'Best First'.
Re: Re: Killing of ALL children made by Open3
by Anonymous Monk on Nov 12, 2003 at 19:51 UTC
    Hello sgifford,

    I modified your code to fit my example by changing one of the my_open3's to:

    my_open3(\*STDOUT,\*STDIN,\*STDOUT,"ping -t localhost");

    I was excited that your code seemed to work off the bat (no more lingering PINGS), unfortunately, it appears that the pings are not even being created, because the CMD.exe spawned by Open3 is being killed before it can spawn the PING (I believe, not sure though).

    I tested this by throwing in a sleep(10); before the my_killkids('INT'); and sure enough PING is created and stays around after all the killing is finished.

    I appreciate the reply, I will continue to work with your code because I believe you are on the right track.

    Thanks,
    Steve

      I don't have any Windows machines to test on; it works on Linux.

      One thing I noticed is that my arguments to open3 were wrong. This works better:

      my_open3(\*STDIN,'>&STDOUT','>&STDOUT',"ping 10.0.0.1"); my_open3(\*STDIN,'>&STDOUT','>&STDOUT',"ping 10.0.0.100"); my_open3(\*STDIN,'>&STDOUT','>&STDOUT',"ping 10.0.0.2");

      Another thing to think about is that CMD.EXE and PING could both be running at the same time, so killing the PID returned by open3 might just kill CMD.EXE and leave PING alone. You might be able to avoid running CMD.EXE by giving open3 ping and its arguments as a list instead of a string:

      my_open3(\*STDIN,'>&STDOUT','>&STDOUT','ping', '10.0.0.2');
      but I'm not sure.
        Hello,

        Well, I have done some quick testing and it appears that seperating out the args, as you suggested, works just fine.

        I will report back if I find anything out of interest, but overall, thank you for your time and effort, it is greatly appreciated.

        Steve