What does kill 0 do actually?

25,612

Like it says, it sends the signal to all the members of the process group of the caller.

Process groups are used to implement job control in the shell (they can be used for other things, but interactive shell job control is the main reason for their existence).

You'll notice that when you type Ctrl-C, all the processes of the current jobs are killed, not only the one that started them. Also, that doesn't kill the background jobs.

That is achieved with process groups. A job is a group of processes started by a shell which the shell can put in background or foreground (set as the foreground process group of the terminal or not), and kill as a whole.

You can find out about process group ids and session ids with ps -j (j for Job control).

To kill the process group of PGID $x, you do:

kill -- "-$x"

kill 0 kills the process group of the caller.

Note that if you do: /bin/kill 0, the shell will start a new job to execute that kill command, so kill will only kill itself.

kill is usually a shell builtin though, so kill will kill the process group of the shell.

However, when the shell is interactive, it is the process managing process groups, so typically there's no other process in the process group of the shell. All the processes started by the shell, are in other process groups:

$ sleep 1000 &
[1] 22746
$ ps -j
  PID  PGID   SID TTY          TIME CMD
22735 22735 22735 pts/23   00:00:00 zsh
22746 22746 22735 pts/23   00:00:00 sleep
22749 22749 22735 pts/23   00:00:00 ps

Above, sleep and ps are in two different process groups, one in background, one in foreground and they are different from the process group of the shell.

You could do though:

(man kill & sleep 1; ps -j; kill 0)

The interactive shell would start a new process group for that subshell, and both the subshell and man (and the other commands started by man like your pager, groff...) would be in the same process group, so kill 0 would work there. (the sleep above is to give enough time for the pager to start so we can see it in the ps -j output before we kill it).

Share:
25,612

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 1 year

    In the man page, it says:

    kill [ -s signal | -p ] [ -a ] [ -- ] pid ...
     pid... Specify the list of processes that kill should signal.  Each pid can be one of five things:
              0      All processes in the current process group are signaled
    

    And I tried like this in bash:

    $ man kill &
    [1] 15247
    $
    [1]+  Stopped                 man kill
    $ kill 0
    $ ps
    15247 pts/41   00:00:00 man
    

    Here 0 is used as pid. As I understood, kill 0 will kill all processes in the current process, which includes pid15247. However, it didn't do anything in this example. Does anyone have ideas about how to use it?

    • Admin
      Admin over 10 years
      It sends a SIGTERM to the process, which it has done. The man command has chosen to ignore it.
    • Admin
      Admin over 10 years
      @jordanm, no. a job started by an interactive shell has a different process group than the shell. That's how the shell can put it in foreground and background. See ps -j to see the process groups.
    • Admin
      Admin almost 7 years
      This question is actually a copy of SuperUser question What does kill 0 do actually? (which was migrated from StackOverflow). See also questions about kill -0 (note the dash), which is a different topic, at StackOverflow and here on Unix.SE.
  • kyb
    kyb about 4 years
    kill -- -$x actually says bash: kill: (-63531) - No such process in bash 5.0.11(1)-release on MacOS 10.14 but it is sure process exists and running. pkill -P works fine
  • Stéphane Chazelas
    Stéphane Chazelas about 4 years
    @kyb $x must be a process group id not process id. See the output of ps -j to find out about pgids