Why can't I kill this process on Linux?
Solution 1:
Problem
Your script is probably creating zombies because of your kill -9
commands; as suggested from jjlin answer too is never a good practice to kill abruptly some process without being forced to.
From man bash
we can read:
Processes marked < defunct > are dead processes (so-called "zombies") that remain because their parent has not destroyed them properly. These processes will be destroyed by init(8) if the parent process exits.
Answer #1: The process init has the PID 1 and for this Linux assigns them the parent with PID 1 (because it assign them to init).
Answer #2: They cannot be killed simply because they are just dead... if their parent is init
probably is enough to wait some time.
To remove zombies from a system, the SIGCHLD signal can be sent to the parent manually, using the kill command. If the parent process still refuses to reap the zombie, the next step would be to remove the parent process. When a process loses its parent, init becomes its new parent. Init periodically executes the wait system call to reap any zombies with init as parent. [1]
Just in case this idea arises one day or another: to #kill -9 init
process with root privilege is the software equivalent to physically unplug the computer from the electricity grid. [:-)]
However zombie processes can be identified in the output of ps
command by the presence of a "Z" in the STAT column. You can use the following line to easily identify them
ps -aux | grep Z
Some references about Linux zombies world:
- Is there any way to kill a zombie process without reboot?
- What are zombie processes
- How to kill zombie process
- [1] From many sources on the net
Solution 2:
To answer question number 1:
When a process spawns child processes, the children each have their own PID. The PPID of each child (parent's process id) is the PID of their parent process. If the parent dies, then the child processes are orphaned. Orphaned processes are automatically picked up by the system init process which has a PID of 1.