Saturday, September 14, 2013

Kill command in UNIX and Linux

Kill command in UNIX and Linux is normally used to kill a suspended or hanged process or process group.

Like in windows when we see a particular process hung the system we go to task-manager find the process and kill it, similarly in UNIX and Linux we first find the process ID (PID) of offending process and then kill it.

1)
kill -9 : is used to forcefully terminate a process in Unix. Here is syntax of kill command in UNIX.

ps -ef| grep java // will give you PID
kill -9 PID

2) kill command to kill multiple processes

Syntax of kill in UNIX for killing multiple processes:

kill -9 pid1 pid 2

3 ) Kill the processes by name
Kill the firefox process.

$ ps -ef | grep firefox
1986 ? Sl 7:22 /usr/lib/firefox-3.5.3/firefox

$ kill -9 1986

4. Pkill Command – Send signal to the process based on its name

You can send signal to any process by specifying the full name or partial name. So there is no need for you to find out the PID of the process to send the signal.

$ pkill sample : kills all the processes with the name "sample"

Pkill Example:
Before sending signal, you can verify which are all the process is matching the criteria using “pgrep -l”, which displays the process ID and process name of the matching processes.

In this example, all the processes are designed to log the signal to signal-log, along with its PID.

$ pgrep -l sample
12406 sample-server.p
12425 sample-server.p
12430 sample-garbagec

$ pkill -USR1 sample

$ cat signal-log
Name: ./sample-server.pl Pid: 12406 Signal Received: USR1
Name: ./sample-server.pl Pid: 12425 Signal Received: USR1
Name: ./sample-garbagecollector.pl Pid: 12430 Signal Received: USR1


No comments:

Post a Comment