5 kill Command Usage Examples in Linux
Posted by Superadmin on February 27 2017 05:49:28

5 “kill” Command Usage Examples in Linux

This tutorial explains Linux “kill” command, options and its usage with examples.

kill – send signals to running processes in order to request the termination of the process.

 

DESCRIPTION

Even on Linux it sometimes happens that processes wear out their welcome and stick around longer than you would like them to. They simply ignore your request to close up and go away. Fortunately you have a powerful gun at your disposal that will clean out anything that doesn’t get a hint: The Arnold Schwarzenegger equivalent among the Linux commands is the kill command.

There are many different signals that can be sent , although the signals in which users are generally most interested are SIGTERM and SIGKILL. The default signal sent is SIGTERM. Programs that handle this signal can do useful cleanup operations (such as saving configuration information to a file) before quitting. However, many programs do not implement a special handler for this signal, and so a default signal handler is called instead. Other times, even a process that has a special handler has gone awry in a way that prevents it from properly handling the signal.

SYNOPSIS

kill [options] […]

OPTIONS

[…]
Send signal to every listed.
–,-s ,–signal 
Specify the signal to be sent. The signal can be specified by using name or number.
-l, –list [signal]
List signal names. This option has optional argument, which will convert signal number to signal name, or other way round.
-L, –table
List signal names in a nice table.

Common UNIX Signal Names and Numbers

NumberNameDescriptionUsed for
0 SIGNULL Null Check access to pid
1 SIGHUP Hangup Terminate; can be trapped
2 SIGINT Interrupt Terminate; can be trapped
3 SIGQUIT Quit Terminate with core dump; can be
9 SIGKILL Kill Forced termination; cannot be trapped
15 SIGTERM Terminate Terminate; can be trapped
24 SIGSTOP Stop Pause the process; cannot be trapped
25 SIGTSTP Terminal stop Pause the process; can be
26 SIGCONT Continue Run a stopped process

Alternate signals may be specified in three ways: -9,-SIGKILL or -KILL. Negative PID values may be used to choose whole process groups; see the PGID column in ps command output. A PID of -1 is special; it indicates all processes except the kill process itself and init.

EXAMPLES

1. Send a Kill Single To Process ID 1414

$ kill 4242

To find pid of any job or command use ps command:

$ ps | grep command
$ ps aux | grep command
$ ps aux | grep apache

2. Forcefully kill 1414 process

$ kill -s SIGKILL 1414

OR

$ kill -s KILL 1414

OR

$ kill -s 9 1414

OR

$ kill -SIGKILL 1414

OR

$ kill -KILL 1414

3. To stop all of your processes and log yourself off, enter the following command

$ kill -kill 0

This sends signal 9, the SIGKILL signal, to all processes that have a process group ID equal to the senders process group ID. Because the shell cannot ignore the SIGKILL signal, this command also stops the login shell and logs you off.

4. To stop all processes that you own, enter the following command

$ kill -9 -1

This sends signal 9, the SIGKILL signal, to all processes owned by the effective user

5. Display ginal names

# kill -L
 1 HUP      2 INT      3 QUIT     4 ILL      5 TRAP     6 ABRT     7 BUS
 8 FPE      9 KILL    10 USR1    11 SEGV    12 USR2    13 PIPE    14 ALRM
15 TERM    16 STKFLT  17 CHLD    18 CONT    19 STOP    20 TSTP    21 TTIN
22 TTOU    23 URG     24 XCPU    25 XFSZ    26 VTALRM  27 PROF    28 WINCH
29 POLL    30 PWR     31 SYS