Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
It sends a SIGTERM to the process, which it has done. The man command has chosen to ignore it. –  jordanm Mar 11 '13 at 5:48
2  
@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. –  Stéphane Chazelas Mar 11 '13 at 8:54

1 Answer 1

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 it to give enough time for the pager to start so we can see it in the ps -j output before we kill it).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.