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;
3

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.

  • I want the script to do the kill process instead of running the command's to kill the process – user3613649 Jun 25 '16 at 21:12
  • @user3613649 Its more cleaner and simpler than running a script for this. If you want you can just create a script using the commands. – heemayl Jun 25 '16 at 21:19
  • An 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 – user3613649 Jun 25 '16 at 21:45
  • @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.. – heemayl Jun 25 '16 at 21:48
  • Thanks. for pid in $(ps axo pid=,stat= | awk '$2~/^Z/ { print $1 }') ; do pid: kill "$(ps -p "$pid" -o ppid=)" echo (Detected zombie process:"$PID". "$user": . Successful); else echo (Detected zombie process:"$PID". "$user": . not successful); done – user3613649 Jun 25 '16 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.