Linuxopsys
Linuxopsys

@linuxopsys

33 Tweets 4 reads Oct 22, 2023
Knowing when and how to stop running processes is a essentialย skill for sysadmins. When a process becomes stuck, it only takes a gentle nudge to restart or stop it.
At times, a process takes all the system resources. In both cases, you need a cmd that lets you manage a process.
The Linux operating system includes a number of commands for terminating errant processes (rogue processes), such as pkill, kill, and killall.
This thread will teach you how to use the kill command in Linux.
๐—š๐—ฒ๐˜๐˜๐—ถ๐—ป๐—ด ๐˜๐—ผ ๐—ธ๐—ป๐—ผ๐˜„ ๐—Ÿ๐—ถ๐—ป๐˜‚๐˜… ๐˜€๐—ถ๐—ด๐—ป๐—ฎ๐—น๐˜€
Linux processes use signals to communicate with one another. A process signal is a predefined message that processes can either ignore or respond to. Developers define how a process will handle signals.
Below are some of the Linux signals that processes can receive and respond to:
๐—ธ๐—ถ๐—น๐—น ๐—ฐ๐—ผ๐—บ๐—บ๐—ฎ๐—ป๐—ฑ ๐—ถ๐—ป ๐—Ÿ๐—ถ๐—ป๐˜‚๐˜…
Most shells, including Bash and zsh, include kill commands. The /bin/kill or /usr/bin/kill executable, as well as the shell's built-in kill, behave in slightly different ways.
To find out all the locations on your system hwere the kill command is located, use the type command and the -a option:
$ type -a kill
The output above shows that when you type kill on the command line, the shell builtin is used rather than the executable binaries. To use the executable binary, enter its full path (for example, /bin/kill or /usr/bin/kill).
In this thread , we will discuss the bash built-in command, so if you are using another shell, you must switch to bash to get similar results.
๐—ธ๐—ถ๐—น๐—น ๐—ฐ๐—ผ๐—บ๐—บ๐—ฎ๐—ป๐—ฑ ๐—ถ๐—ป ๐˜€๐˜†๐—ป๐˜๐—ฎ๐˜…
The kill command has the following syntax:
$ kill [options] <pid> [...]
You can use the kill command to send signals to processes based on their process IDs (PIDs). By default, when the kill command is used without specifying the signal, all of the PIDs specified on the command line receive a SIGTERM signal.
The following are the most commonly used signals:
- 1 (SIGHUP) - Hangs up the process.
- 9 (SIGKILL) - Unconditionally terminates the process.
- 15 (SIGTERM) - Stop a running process gracefully.
Use the kill command with the -l option to see a list of all the available signals that can be sent with the kill command:
$ kill -l
You must be the process owner or logged in as the root user to send a process signal. Ordinary users can only send signals to their own processes. To send signals to the process of other users you must be a root user.
You can use the -s option to specify which process signals to send by using their name or signal number. There are several ways to specify signals, as shown below:
- Using the equivalent signal number (-1 or -s 1).
-Prefixing the signal name with โ€œSIGโ€(-SIGHUP or -s SIGHUP).
- Omitting the โ€œSIGโ€ prefix (-HUP or -s HUP).
So go ahead and select the option that works best for you.
๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ณ๐—ถ๐—ป๐—ฑ ๐—Ÿ๐—ถ๐—ป๐˜‚๐˜… ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€๐—ฒ๐˜€ ๐—ถ๐—ป๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป
When a program runs on the system, it is referred to as a process. To get a glimpse of these processes, you must first become acquainted with ps command.
This command can display a large amount of information about all of the programs currently running on your system.
Here is an example of ps basicย syntax:
$ ps
As you can see, the default ps doesn't provide a lot of information. The cmd, by default, only displays processes that are currently running on the current terminal and are owned by the current user. In this case, only the bash shell, the zsh shell, and the ps cmd were running.
The basic output shows the process ID (PID) of the application, the terminal (TTY) from which it is being executed, and the amount of CPU time each process has consumed. To view more information about the programs, combine the ps with several options.
If you want to learn more about the various options available with ps, simply type 'man ps' into your terminal.
$ man ps
Despite being excellent for learning about the processes that are active on the system, ps has one drawback. This command displays only information for a specific time period.
The ps command makes identifying patterns in processes that switch in and out of memory difficult. Instead, the top command is useful.
The top command, like the ps command, displays process information in real time. Here's an example of a top showing real-time processes.
๐—ธ๐—ถ๐—น๐—น ๐—ฐ๐—ผ๐—บ๐—บ๐—ฎ๐—ป๐—ฑ ๐—ผ๐—ฝ๐˜๐—ถ๐—ผ๐—ป๐˜€
Here is a list of the most common kill command otpions you should be aware of:
๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ธ๐—ถ๐—น๐—น ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€๐—ฒ๐˜€ ๐—ถ๐—ป ๐—Ÿ๐—ถ๐—ป๐˜‚๐˜…
To terminate a process with the kill command, you must first obtain its process ID (PID). To accomplish this, various commands such as ps, pgrep, top (or other top variants such as htop, btop++, etc.), and pidof can be used
Now that you understand signals and how to locate processes on your system, let's use the kill command to terminate a process.
๐—ž๐—ถ๐—น๐—น ๐—ฎ ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€ ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€๐—ฒ๐˜€ ๐—œ๐——
Assume you need to terminate ย zshย process because it is no longer responding. Its PIDs can be found using the pgrep command. The pgrep command is used to determine the process id of a particular process
$ pgrep zsh
Notice the above command prints the process IDs of all the zsh processes:
You can now then kill all processes once you know their PIDs by sending a KILL signal (SIGKILL or -9) :
$ kill -9 6698 6792 6816 6846
Here the kill command sends the kill signals to all the processes that belong to the vscode program.
๐—ž๐—ถ๐—น๐—น ๐—ฎ ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€ ๐˜‚๐˜€๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€ ๐—ป๐—ฎ๐—บ๐—ฒ
The kill command has a significant disadvantage in that you cannot kill processes by name. The pkill command is an excellent tool for terminating processes by name rather than PID number.
However, there is a workaround for the kill command by combining the above command with the kill command.
$ kill -9 $(pgrep firefox)
You can also achieve the same thing with pidoff as shown:
$ kill -9 $(pidof firefox)
This way the kill command will send the specified signal to all vscode processes.
๐—ฆ๐˜‚๐—บ๐—บ๐—ถ๐—ป๐—ด ๐˜‚๐—ฝ!
In this tutorial, we learned about the kill command in Linux and how to use it. For more information visit the kill man page or simply type "man kill" from the terminal.
That's it for this thread:
If you found this thread valuable:
1. Toss us a follow for more daily threads on Linux, sysadmin, and DevOps โ†’ @linuxopsys
2. Like and RT the first tweet so other Linux folks can find it too.

Loading suggestions...