kill Command Examples in Linux

Different commands are used to send signals to processes to terminate or “kill” them. This is necessary when a process becomes unresponsive (hangs), causes system instability, or fails to relinquish control over a file you’re trying to modify.

Using the pid number to terminate processes

You can use the kill command with the process table to end processes. By entering kill followed by the PID, you can terminate specific processes. However, the process table may display processes that do not belong to you. As a user, you can use the kill command only with processes that you own. As root, you can kill any process.

Kill Signals

There are many ways to kill a process, each one mapped to a signal. This signal determines how to kill the process. Some signals are more appropriate than others in certain situations. For example, you may wish to terminate a process gracefully, giving it time to clean up any last activities. However, some processes will ignore this signal or simply fail to terminate in a reasonable amount of time. For that, you may have to issue a more aggressive signal.

There are many different kill signals. Each signal has a name and one or more corresponding number values; you can use either with the kill command. Some of the most common signals are described below:

1. SIGINT (2)
– Interrupts process from terminal.
– Can be caught or ignored.
– Same as pressing Ctrl+C

2. SIGKILL (9)
– Kills process immediately.
– Cannot be caught or ignored.
– Last resort.

3. SIGTERM (15)
– Terminates process.
– Can be caught or ignored.
– Sent from program and not terminal.

4. SIGSTOP (17, 19, 23)
– Pause process.
– Cannot be caught or ignored.
– Sent from program and not terminal.

5. SIGSTP (18, 20, 24)
– Pauses process from terminal.
– Can be caught or ignored.
– Same as pressing Ctrl+Z.

Demo of kill signals

The following are some examples of implementing kill signals. To terminate a process with ID 921 gracefully:

$ kill 15 921

Failing that, to kill the process immediately:

$ kill 9 921

Alternatively, to pause rather than remove the process entirely:

$ kill 17 921

kill Command Examples

1. To get the list of signals:

# kill -l 
 1) SIGHUP  2) SIGINT      3) SIGQUIT  4) SIGILL
 5) SIGTRAP  6) SIGABRT      7) SIGEMT  8) SIGFPE
 9) SIGKILL 10) SIGBUS     11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM     15) SIGTERM 16) SIGURG
17) SIGSTOP 18) SIGTSTP     19) SIGCONT 20) SIGCHLD
21) SIGTTIN 22) SIGTTOU     23) SIGIO 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGINFO 30) SIGUSR1     31) SIGUSR2

2. To send a particular signal:

# kill -s 9 

3. To specify the list of processes for kill:

# kill 1234 2345 3456 

4. Terminate a program using the default SIGTERM (terminate) signal:

# kill process_id

5. Terminate a background job:

# kill %job_id

6. Terminate a program using the SIGHUP (hang up) signal. Many daemons will reload instead of terminating:

# kill -1|HUP process_id

7. Terminate a program using the SIGINT (interrupt) signal. This is typically initiated by the user pressing `Ctrl + C`:

# kill -2|INT process_id

8. Signal the operating system to immediately terminate a program (which gets no chance to capture the signal):

# kill -9|KILL process_id

9. Signal the operating system to pause a program until a SIGCONT (“continue”) signal is received:

# kill -17|STOP process_id

10. Send a `SIGUSR1` signal to all processes with the given GID (group id):

# kill -SIGUSR1 -group_id
Related Post