Why doesn’t the kill command kill the process?
October 30, 2021
The kill command is used to stop a process, the command syntax is:
sudo kill PROCESS-ID
You may run into a situation when using kill does not kill the process.
You can get the process ID knowing the name of the executable file with the command:
ps -e | grep 'NAME'
The same command can be used to check if the process is still running.
Related article: How to use ps command to monitor Linux processes
The kill command is usually sufficient to kill most processes. But it may be that some process does not react to ‘kill’ command at all, even if you run it with sudo.
The name of the “kill” command continues to mislead many, many users (including myself at the beginning). It is assumed that when you say “kill X” it means exactly “stop (kill) the process X”. But in reality, this is far from the case. The kill command just sends one of the signals to the process.
If kill is called without any parameters, it sends signal number 15 (SIGTERM). This signal can be ignored by the process. This signal notifies a process to put its things in order, and then the process itself exits correctly. This is a good way.
You can also “send” signal number 9 (SIGKILL), which the process cannot ignore. The process doesn't even recognize it, because the kernel ends the process, not the process itself. This is an evil way.
Processes can ignore some signals. If you send a SIGKILL, it will not be able to ignore it and will not perform preparatory actions to complete, such as saving data or cleaning up.
kill -9 PROCESS-ID
If you suspend a process with CTRL-z, it will ignore most of the signals while it is suspended (that is, until you fg or bg on the process).
Also note that in some very specific circumstances a process can be in a zombie/non-functional state that even SIGKILL cannot kill the process. In this case, you will need to find the parent process and kill the parent process.
Some say kill -9 <PID> always works. It's a delusion. There are situations where even kill -9 does not kill the process. For example, when the process is in state D (continuous sleep). The process enters this state every time it waits for I/O (usually not very long). So, if a process is waiting for I/O (for example, on a faulty hard disk) and it is not properly programmed (with a timeout), then you simply cannot kill the process. Whatever you do. You can simply try to make the file available to keep the process going.
- What are the differences and how to use the kill, pkill and killall commands (99.4%)
- How to use the top command to monitor Linux processes (76.3%)
- htop command guide: how to view processes in Linux interactively (76.3%)
- How to use ps command to monitor Linux processes (76.3%)
- Do services need to be restarted when updating packages (57.7%)
- How to determine the type and speed of a USB port (RANDOM - 50%)