8 Useful Commands to Kill a Process in Linux

8 Useful Commands to Kill a Process in Linux

Sometimes, when a program running on your Linux system is unresponsive or using too many resources, you may need to terminate it manually. This is where the process of killing a process (or stopping a program) comes in handy. In Linux, there are several ways to do this, and we’ll explore them in this article.


How to View Processes in Linux

Before you can kill a process, you need to know its process ID (PID). You can find this out by using the ps command. The ps command lists the current processes running on your Linux system, along with their PIDs and other information.

For example, to see all the running processes on your system, you can run the following command:

$ ps -aux

This will display a list of all running processes, along with their PIDs and other information. Here’s an example output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 168976  9632 ?        Ss   Feb14   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S    Feb14   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   Feb14   0:00 [rcu_gp]
...

System Kill Signals in Linux

The kill, killall, and pkill commands are used to send signals to specified processes or process groups in Linux. When no signal is specified, each of these commands sends the default signal SIGTERM (15) to the process. However, different signals can be specified to achieve different effects.

The most commonly used signals include:

  1. SIGHUP (1) – Reload a process.
  2. SIGKILL (9) – Kill a process immediately.
  3. SIGTERM (15) – Gracefully stop a process.

These signals can be specified in three different ways – using a number, with the “SIG” prefix, or without the “SIG” prefix. For example, you can use any of the following to send a SIGHUP signal to a process with a PID of 1234:

$ kill -1 1234
$ kill -SIGHUP 1234
$ kill -HUP 1234

The kill command can be used to send signals to individual processes by specifying the process ID (PID). For example, to send a SIGKILL signal to a process with a PID of 5678, you can run:

$ kill -9 5678

The killall and pkill commands can be used to send signals to processes that match a particular name or other criteria. For example, to send a SIGTERM signal to all processes with a name of “firefox”, you can run:

$ killall -15 firefox
$ pkill -15 firefox

To see a list of all available signals, you can use the -l option with the kill or killall command:

kill -l  # or killall -l

Output:

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Using these commands with the appropriate signal can help you manage and terminate processes more effectively in Linux.


How to View Processes in Linux

Before you can kill a process, you need to know its process ID (PID). You can find this out by using the ps command. The ps command lists the current processes running on your Linux system, along with their PIDs and other information.

For example, to see all the running processes on your system, you can run the following command:

$ ps -aux

This will display a list of all running processes, along with their PIDs and other information. Here’s an example output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 168976  9632 ?        Ss   Feb14   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S    Feb14   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   Feb14   0:00 [rcu_gp]
...

How to Kill a Process Using the kill Command in Linux

The kill command is a command-line utility used to send signals to processes, which can be used to terminate a process. By default, the signal sent is SIGTERM, which allows the process to perform cleanup operations before terminating. If the process does not respond to SIGTERM, the SIGKILL signal can be sent, which immediately terminates the process.

To kill a process using the kill command, you need to know its PID. For example, to kill a process with PID 1234, you can run the following command:

$ kill 1234

If the process does not respond to SIGTERM, you can use the -9 option to send the SIGKILL signal instead. For example:

$ kill -9 1234

Here’s an example of killing a process with PID 1234 using the kill command:

$ ps -aux | grep firefox
johndoe  1234  4.0  5.0 123456 78910 ?       R    Feb15   2:01 /usr/bin/firefox
$ kill 1234
$ ps -aux | grep firefox

In this example, we first used the ps command with the grep option to find the PID of a process called “firefox“. We then used the kill command to send the SIGTERM signal to the process with PID 1234. Finally, we used the ps command again to confirm that the process was terminated (i.e., it no longer appears in the output).


How to kill a Process Using the pkill Command in Linux

The pkill command is used to search for processes based on their name and send signals to them. This command is useful when the PID of the process is not known.

For example, to kill all processes with the name “firefox”, you can run the following command:

$ pkill firefox

This will send the SIGTERM signal to all processes with the name “firefox”.

Here’s an example of killing all processes with the name “firefox” using the pkill command:

$ ps -aux | grep firefox
johndoe  1234  4.0  5.0 123456 78910 ?       R    Feb15   2:01 /usr/bin/firefox
johndoe  5678  2.0  3.0 234567 89012 ?       S    Feb15   1:30 /usr/bin/firefox
$ pkill firefox
$ ps -aux | grep firefox

In this example, we first used the ps command with the grep option to find the PIDs of all processes with the name “firefox”. We then used the pkill command to send the SIGTERM signal to all these processes. Finally, we used the ps command again to confirm that all processes with the name “firefox” were terminated (i.e., they no longer appear in the output).


How to Kill a Process by Name Using killall in Linux

The killall command is similar to pkill, but it sends a signal to all processes with a given name. To kill all processes with the name “firefox” using killall, you can run the following command:

$ killall firefox

In this example, the killall command was executed to kill all processes with the name “firefox”. However, since there were no processes with that name running, the command returned an error message indicating that no process was found.


How to Kill a Process Using its Command Line in Linux

Sometimes, you may need to kill a process based on its command line rather than its name or PID. For example, you may want to kill all instances of a script that is causing problems. You can do this using the pkill command with the -f option, which matches the entire command line. For example, to kill all instances of a script called “myscript.sh”, you can run the following command:

$ pkill -f myscript.sh

Output:

[1]+  Terminated              ./myscript.sh

In this example, the pkill command was executed with the -f option to match the entire command line of the process. The command successfully terminated a process with a PID of 1, which was running a script called “myscript.sh”. The output also shows the message “Terminated” to confirm that the process was successfully killed.


How to Kill a Process with SIGKILL in Linux

As mentioned earlier, the SIGKILL signal is a powerful signal that immediately terminates a process. However, it should only be used as a last resort, as it can result in data loss or corruption. To send a SIGKILL signal to a process with a PID of 1234, you can run the following command:

$ kill -9 1234

In this example, the kill command was executed with the -9 option to send a SIGKILL signal to the process with a PID of 1234. Since the SIGKILL signal immediately terminates the process, no output is produced by the command.


How to Kill a Process and its Child Processes in Linux

Sometimes, a process may have child processes that need to be terminated as well. You can use the pkill command with the -P option to specify that the signal should be sent to the process and its child processes. For example, to kill a process with a PID of 1234 and its child processes, you can run the following command:

$ pkill -P 1234

In this example, the pkill command was executed with the -P option to send the signal to the process with a PID of 1234 and its child processes. Since no output is produced by the command, you can use the ps command to confirm that the process and its child processes have been terminated.


How to Kill a Process Using a Custom Signal in Linux

In addition to the signals mentioned earlier, there are several other signals that can be used to terminate a process. You can use the kill command with the -s option to specify a custom signal to send to the process. For example, to send a SIGUSR1 signal to a process with a PID of 1234, you can run the following command:

kill -s SIGUSR1 1234

In this example, the kill command was executed with the -s option to send a SIGUSR1 signal to the process with a PID of 1234. Since no output is produced by the command, you can use the ps command to confirm that the process has been terminated.


How to Kill a Process Owned by a Specific User in Linux

If you need to kill a process that is owned by a specific user, you can use the pkill command with the -u option to specify the user. For example, to kill all processes owned by the user “johndoe”, you can run the following command:

$ pkill -u johndoe

In this example, the pkill command was executed with the -u option to specify the user “johndoe”. The command successfully terminated all processes owned by that user, and no output is produced by the command.


Conclusion

Killing a process is a common task in Linux, especially when dealing with unresponsive or resource-hogging programs. In this article, we’ve explored several ways to kill a process, including using the kill and pkill commands. Remember to use caution when killing processes, especially with the SIGKILL signal, as it can result in data loss or corruption.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top