Can not kill process by kill -9? - linux

I try to kill process by using command "kill -9 pid", but can not succeed. Anybody know how could I kill such process and why I can't kill it ?

The process could be zombie? Its good to check process state using ps command as well if you have permission.

If your process is in an uninterruptable sleep (D) due to hanging in some hardware access, you indeed cannot terminate that process.
Here is another explanation.
Personally, I saw such D states for example when accessing files on a SD card or USB stick when there was a hardware problem. But there are many other scenarios where such a state might occur.

Related

Problems with killing jobs

I would like to kill a job serial, which contains several calculations. With the command 'kill PID', where PID refers to process ID, the currently running calculation cancels, but the process has not been stopped. Instead, the next calculation starts, but I would like to kill the entire job, the entire process.
The kill -9 <pid> should do the job. Unfortunately this might not always work if program is poorly programmed you might be unable to kill it.

What special precautions must I make for docker apps running as pid 1?

From what I gather, programs that run as pid 1 may need to take special precautions such as capturing certain signals.
It's not altogether clear how to correctly write a pid 1. I'd rather not use runit or supervisor in my case. For example, supervisor is written in python and if you install that, it'll result in a much larger container. I'm not a fan of runit.
Looking at the source code for runit is intersting but as usual, comments are virtually non-existent and don't explain what's being done for what reason.
There is a good discussion here:
When the process with pid 1 die for any reason, all other processes
are killed with KILL signal
When any process having children dies for any reason, its children are reparented to process with PID 1
Many signals which have default action of Term do not have one for PID 1.
The relevant part for your question:
you can’t stop process by sending SIGTERM or SIGINT, if process have not installed a signal handler

How does linux kill D status process during reboot?

I know ths D status processes is uninterruptable sleep processes.
Many people say to kill D status processes is to reboot the system.
But how does reboot operation can kill the D status processes?
I find "init 0" will "kill -9 " all of the processes at last. But "kill -9 " can not kill D status process.
Someone tell me how?
It does not kill them at all. Those processes in D state will not respond to any signal. kill generates signals -- they cannot be delivered to these processes. So, no kill.
The loss of process context when the kernel stops running allows nothing to persist, processes are kernel objects. The state D processes become history at that point.
If you see this often it usually means some kind of hardware problem, like a cdrom/DVD device. The D state means the process is blocking on some uninterruptable operation on a device.
This is a good question!
I use the yum update command to kill the D state process.

Are there suspend\resume signals in Linux?

My application needs to react on hibernation mode so it can do some action on suspending and other actions on resuming. I've found some distributive-specific ways to achieve it(Upower + DBus) but didn't find anything universal. Is there a way to do it?
Thanks!
A simple solution to this is to use a self-pipe. Open up a pipe and periodically write timestamps to it. select on this pipe to read the timestamps and compare them to the current time. When there is a big gap, that means you have just woken up from system suspension or hibernate mode.
As for the other way around, there is not much time when the lid is closed and it flips the switch.
If you really need to act on suspend, then you will need to set powersave hooks like this https://help.ubuntu.com/community/PowerManagement/ReducedPower in pm-utils. It could be as simple as
kill -1 `cat mypid` ; sleep 1
Your process would then trap SIGHUP and do what needs to be done to prepare for suspension. The sleep delays the process long enough for your program to react to the signal.
I believe you are looking for SIGSTOP and SIGCONT signals. You can send these to a running process like so:
kill -STOP pid
sleep 60
kill -CONT pid

Auto kill process in Linux

How can I do this task: if I kill a process, that process and other processes will all die ?
Kill the init process.
If you kill the parent process, child processes will remain and become a zombie.
There are many nice answers about process termination under Linux in general here : Best way to kill all child processes
Since I'm not too sure what you mean with that ambiguous question, I'm afraid this is the best answer I can give you.

Resources