Zombie process (kill both child and parent process)
You can't kill a Zombie, its already dead. It is just talking an entry in the process table before the parent process do wait(2)
to read it's exit status.
On a different note, to kill the parent process of any process (including Zombie), you can easily use a combination of commands ps
and kill
:
ps -p <pid> -o ppid=
will give use the PPID (Parent Process ID) of the process having PID (Process ID) <pid>
.
So for example, to find the PPID of process having PID 2345
:
ps -p 2345 -o ppid=
You can pass it to kill
using command substitution $()
:
kill "$(ps -p 2345 -o ppid=)"
On the other hand to kill a process using its PPID, use pkill
:
pkill -P <PPID>
For example to kill the process having PPID 1234
:
pkill -P 1234
Also unless absolutely necessary do not use SIGKILL
(kill -9
) as it does not let the process to do any cleanup and might result in unwanted effects.
Related videos on Youtube
user3613649
Updated on September 18, 2022Comments
-
user3613649 over 1 year
Question: I want to make sure that both child and parent process are killed and echo if successful or not. I'm new on using bash script and having issue with my output.
#!/bin/bash for p in $(ps jauxww | grep Z | grep -v PID | awk '{print $3}'); do for everyone in $(ps auxw | grep $p | grep cron | awk '{print $2}');do kill -9 $everyone; echo(Detected zombie process:"$PID". "$usr": . Successfully Killed); else echo (Detected zombie process:"$PID". "$usr": . Could not kill); done; done;
-
user3613649 almost 8 yearsI want the script to do the kill process instead of running the command's to kill the process
-
heemayl almost 8 years@user3613649 Its more cleaner and simpler than running a script for this. If you want you can just create a script using the commands.
-
user3613649 almost 8 yearsAn example to echo result in a log file for pid in $(ps axo pid=,stat= | awk '$2~/^Z/ { print $1 }') ; do kill "$(ps -p -o ppid=)" echo echo (Detected zombie process:"$PID". "$usr": . Successfully Killed); else echo (Detected zombie process:"$PID". "$usr": . Could not kill); done
-
heemayl almost 8 years@user3613649 You forgot to put the
pid
:kill "$(ps -p "$pid" -o ppid=)"
..Also correct your wording as you can't (aren't) killing a zombie but its parent..