Pprograms like killall do not help me a lot.
But this one helps me - each day.
#!/usr/bin/perl my $prog = shift || die "no args!\n"; my @pids; open PH, "ps ax|" or die $!; while( <PH> ){ /$prog/ || next; /^\s*(\d+)/ and push @pids, $1 } # Updated (thanks to merlyn): kill 15, $_ for@pids; sleep 5; kill 2, $_ for@pids; sleep 5; kill 9, $_ for@pids;


Replies are listed 'Best First'.
Re: Process termination
by merlyn (Sage) on Aug 13, 2005 at 14:00 UTC
    Please please. Not "kill 9"! That gives a process no chance to clean up. Send it a 15, wait 5 seconds, and if it still doesn't die, try a 2, then wait another 5 seconds. If that doesn't work, "9" is then OK.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Process termination
by tlm (Prior) on Aug 13, 2005 at 15:44 UTC

    Accept pkill(1) into your life.

    the lowliest monk

Re: Process termination
by sh1tn (Priest) on Aug 13, 2005 at 23:04 UTC
    tlm - please, give argments before sm.th. 'smart'.
    And ( if possible) do not ban my reply.


      please, give argments

      I'm not tlm.

      pkill(1) offers the name based kill at the unix shell level. As you resort to that level for the kill anyway, it may save you some perl programming... or more convincingly - to use perl at all for that specific task.

      Cheers, Sören

        That's right - there are UNIX tools.
        Is it better to work with your own $HOME/bin/* tools or to expect all programs
        to be installed on all machines?


Re: Process termination
by aplonis (Pilgrim) on Aug 21, 2005 at 02:47 UTC

    Tried and liked it, except for one little problem. Sometimes it would kill itself first, and being thus dead, fail to terminate its target file. This would happen in NetBSD because the arg also appeared in the list and sometimes ahead of the target file.

    So I made a tiny change to exclude the kill script itself as shown below.

    #!/usr/pkg/bin/perl -w my $prog = shift || die "no args!\n"; my @pids; open PH, "ps ax|" or die $!; while( <PH> ){ !/$0/ && /$prog/ || next; # <-- Change here. /^\s*(\d+)/ and push @pids, $1 } # Try gracefully first, then less so. kill 15, $_ for @pids; sleep 5; kill 2, $_ for @pids; sleep 5; kill 9, $_ for @pids;
Re: Process termination
by jfroebe (Parson) on Sep 10, 2005 at 15:23 UTC

    Hi,

    Prior to each kill line, you should check to see if the process is still there. While unlikely, it is possible that the pid number was reassigned to new process. Adding this little safety net may save your skin one day :)

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1