Mohan
Mohan

Reputation: 141

Kill command in UNIX

If I execute the Following command all the process will be killed and the account is logged out.

Command:

kill -9 -1

It is similar to the logout command. In Unix, no such process having the pid as "-1". So, what is the reason for this result?

Upvotes: 0

Views: 579

Answers (3)

geirha
geirha

Reputation: 6181

When the pid argument of kill is prefixed with a -, it sends the signal to the process group instead. Every process has a pid, but also a process group id.

You can see this by running this:

$ bash -c 'sleep 10 & sleep 1; ps -o pid,ppid,pgrp,cmd'
  PID  PPID  PGRP CMD
22471 22467 22471 -bash
22496 22471 22496 bash -c sleep 10 & sleep 1; ps -o pid,ppid,pgrp,cmd
22497 22496 22496 sleep 10
22499 22496 22496 ps -o pid,ppid,pgrp,cmd

Here you can see the bash -c has gotten its own process group (22496, same as its PID), and its two child processes (sleep and ps), has inherited this process group.

kill -TERM 22496 would send a TERM signal to the bash -c process with pid 22496, while kill -TERM -22496 would send a TERM signal to the three processes bash -c, sleep and ps.

-1 however, is a special case, that sends the signal to all processes you can send to.

Upvotes: 2

Ed Heal
Ed Heal

Reputation: 59997

See the manual page (http://linux.die.net/man/2/kill)

If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init), but see below.

So I guess that is you out of the system - but life continues for the rest of us

PS: It is not graceful

Upvotes: 3

Utkarsh Srivastava
Utkarsh Srivastava

Reputation: 9

kill -9 -1 eliminates the need to hard reboot the system and is mainly useful to efficiently and cleanly terminate programs that have are not responding.

Upvotes: -1

Related Questions