Thursday, November 27, 2014

Kill Command Examples in Linux



The Kill command in unix or linux operating system is used to send a signal to the specified process or group. If we dont specify any signal, then the kill command passes the SIGTERM signal. We mostly use the kill command for terminating or killing a process. However we can also use the kill command for running a stopped process.

The syntax of kill command is
kill [-s signal] pid
kill -l

The options to the kill command are:
  • pid : list of process that kill command should send a signal
  • -s signal : send the specified signal to the process
  • -l : list all the available signals.

Let see some of the useful kill command examples in unix or linux system.

Kill Command Examples:

1. Listing all the signal names.

Run the kill command with -l option to list all the available signal names.
> kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2
CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING
LWP FREEZE THAW CANCEL LOST RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2
RTMAX-1 RTMAX

Some the important signals which we use in our daily usage ar listed below:
Number
Signal Name
Description
0
SIGNULL
Used to check access to the process id
1
SIGHUP
Hup signal. Terminates the process.
2
SIGNINT
Interrupt signal. Terminating the process
3
SIGQUIT
Quit signal. Terminate process with core dump
9
SIGKILL
Forcibly killing a process
24
SIGSTOP
Pausing the process
26
SIGCONT
Runs a stopped process

To know more about a signal, check in man pages. To know about the signal 9, run the below man command:
man 5 signal

2. Getting the process id

To know the process id of a process running in the unix system, use the ps command as
ps -aef
root  4529   657   0   Jul 20 ?           0:06 /usr/local/sbin/sshd -R
....
....

The second field in the output is the process Id. Here the /usr/local/sbin/sshd -R is running with the process id 4529.

3. Killing a process.

To kill processes simply pass the process id to the kill command. This is shown below:
kill 4529

4. Forcefully killing a process.

Use the -9 option with the kill command to kill a process force fully. The following kill command terminates the process forcefully:
kill -9 1567
kill -SIGKILL 1567
kill -KILL 1567
kill -s SIGKILL 1567
kill -s KILL 1567

Avoid using the kill -9 for terminating a process. This will cause memory leaks in the operating system and leads to many other issues.

No comments:

Post a Comment