4

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;
Cristiana Nicolae
  • 4,570
  • 10
  • 32
  • 46

1 Answers1

4

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.

heemayl
  • 93,925