Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Posts
    191

    Unanswered: Linux: How to kill all childs running in background?

    Hi,

    in AIX, I believe I can kill off all background processes I started with killall, like:

    $ my-db-stress-script &
    $ my-db-stress-script &

    $ killall

    But I have great trouble to come up with a similar solution for Linux, even within pdksh in linux.

    Does anybody know if there is a command in Linux and AIX that lets me kill my background processes easily, and if not, how to do this in Linux alone?

    Johann

  2. #2
    Join Date
    Oct 2003
    Location
    Slovakia
    Posts
    482

    Re: Linux: How to kill all childs running in background?

    Originally posted by jsander
    Hi,

    in AIX, I believe I can kill off all background processes I started with killall, like:

    $ my-db-stress-script &
    $ my-db-stress-script &

    $ killall

    But I have great trouble to come up with a similar solution for Linux, even within pdksh in linux.

    Does anybody know if there is a command in Linux and AIX that lets me kill my background processes easily, and if not, how to do this in Linux alone?

    Johann
    1. killall -9 my-db-stress-script
    2. ps -aux | grep my-db-stress-script and then kill -9 pid (process id)

  3. #3
    Join Date
    Feb 2003
    Posts
    4

    Re: Linux: How to kill all childs running in background?

    Originally posted by jsander
    Hi,

    in AIX, I believe I can kill off all background processes I started with killall, like:

    $ my-db-stress-script &
    $ my-db-stress-script &

    $ killall

    But I have great trouble to come up with a similar solution for Linux, even within pdksh in linux.

    Does anybody know if there is a command in Linux and AIX that lets me kill my background processes easily, and if not, how to do this in Linux alone?

    Johann
    You should be able to do a simple shell script like:

    for i in `ps -ef|grep my-db-stress-script|awk '{print $2}'`
    do
    kill -9 $i
    done

    This should work on most variants of UNIX.

  4. #4
    Join Date
    Apr 2003
    Posts
    191

    Re: Linux: How to kill all childs running in background?

    Hi ika and astralvoid,

    that's along the lines I settled with, but I would like to do something safer than search the process list.

    So I take it there is no system command to say something like

    kill that process and all its childs in a single stroke

    in linux?

    Johann

  5. #5
    Join Date
    Sep 2003
    Posts
    7
    If you do an fg you can bring the process to the foreground.
    if you do a bg you can put it to the background.

    Eg
    # somescript &
    [1] 17921

    # kill %1

    will kill that process.

    Srikanth

  6. #6
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35
    Originally posted by skrishnaswamy
    If you do an fg you can bring the process to the foreground.
    if you do a bg you can put it to the background.

    Eg
    # somescript &
    [1] 17921

    # kill %1

    will kill that process.

    Srikanth
    That's a good way to kill one process, but if there's multiple background jobs, some of which you don't want to kill, it'll not work.

    The shell should have a 'jobs' command which lists all the commands running in the background for this instance of the shell, rather than the machine's whole process table. You can munge that output and kill the relevant ones:

    for i in `jobs | grep my-db-stress-script | sed -e 's/\].*$//' -e 's/^.*\[//'`; do eval kill %$i; done

    For killing a particular process, "kill <pid>" works as in most *nix.

    "kill -9" is too aggresive a command to remove a process and all it's children, it kills the process immediately. "man kill" or "man signal" should give you more info on the severity of signals.

  7. #7
    Join Date
    Sep 2003
    Posts
    7
    hmmm....on linux you could also use the pstree -p <username> command to find your bg process ,pipe them to a sed and kill em. In that way you wont miss the child pids forked by your background processes and would not reult in Zombie processes.

  8. #8
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35
    There's a race condition in there, skrishnaswamy. What would happen if, between the output of pstree and the kill commands, one of the processes forks a child? pstree won't have listed it.

    It's better to use the kernel to find all of the process' children (its response to the signal) rather than a userspace utility.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •