Zombie processes appear in parent-child process relationships. They are finished executing, but they are still in the process table. For example, when a child process finishes executing, but parent process didn't acknowledge it yet.
I created a zombie process using C and then run this command:
bat zombie.c gcc zombie.c ./a.out & ps aux | grep "defunct" | grep -v "grep"
#include #include #include int main() { pid_t p = fork(); if (p == 0) { exit(0); } else { sleep(2); } } rinri 178372 0.0 0.0 0 0 ? Z 17:11 0:00 [a.out]
So, using the last command I can find zombie processes. Let's say that I know that there is only one zombie process. To kill it, let's kill its parent process:
kill -9 $(ps -o ppid= -p $(ps aux | grep -m 1 "defunct" | grep -v "grep" | awk '{print $2;}'))
By using this line, it's possible to create script to kill all the zombie processes.
kill sends the specified signal to the given pid. killall sends the specified signal to all the processes by the given process name. pkill sends the specified signal to all the processes matching the given pattern.
top -b -n 1 | head -n 5
top - 17:29:37 up 52 min, 1 user, load average: 0.97, 1.02, 1.18 Tasks: 283 total, 1 running, 282 sleeping, 0 stopped, 0 zombie %Cpu(s): 3.8 us, 3.0 sy, 0.0 ni, 92.5 id, 0.0 wa, 0.0 hi, 0.8 si, 0.0 st MiB Mem : 14886.6 total, 9247.6 free, 3192.2 used, 2446.7 buff/cache MiB Swap: 8243.0 total, 8243.0 free, 0.0 used. 11272.0 avail Mem
(Explaining the first evaluation, results may differ after export) The Tasks line says, that there is 283 total processes, 1 of them is currently running, others are sleeping. No stopeed and no zombie processes. The %Cpu(s) line says about processor utilization. (Taken from the Lab6 file) us is the percent of time spent running user processes. sy is the percent of time spent running the kernel. ni is the percent of time spent running processes with manually configured nice values. id is the percent of time idle (if high, CPU may be overworked). wa is the percent of wait time (if high, CPU is waiting for I/O access). hi is the percent of time managing hardware interrupts. si is the percent of time managing software interrupts. st is the percent of virtual CPU time waiting for access to physical CPU. Values such as id, wa, and st help identify whether the system is overworked.
bash -c "exec -a fun${RANDOM}process sleep infinity" & bash -c "exec -a fun${RANDOM}process sleep infinity" & bash -c "exec -a fun${RANDOM}process sleep infinity" & FUNPROCS="$(pgrep -f 'fun.*process.*infinity')" echo "Found $(echo "$FUNPROCS" | wc -l) processes:" echo "$FUNPROCS" for FUNPROC in $FUNPROCS do kill -9 "$FUNPROC" echo "killed $FUNPROC" done
Found 3 processes: 394357 394358 394359 killed 394357 killed 394358 killed 394359
bat helloworld.sh
#!/bin/bash trap 'echo "Interrupt received"; exit' SIGUSR1 while : do echo "Hello World" sleep 10 done
To kill: kill -s SIGUSR1 "$( aux | grep "helloworld.sh" | grep -v "grep" | awk '{print $2)"
bat status.sh
while : do CPUUSAGE="$(top -b - n 1 | grep "Cpu" | awk '{print 100-$8}')%" MEMUSAGE="$(free -h | grep "Mem" | awk '{print $3/$2*100}')%" # disk usage for root directory DISKUSAGE="$(df -h | awk '{ if ($6 == "/") print $5; }')" echo "$(date) CPU: $CPUUSAGE Mem: $MEMUSAGE Disk:$DISKUSAGE" >> /var/log/system_utilization.log sleep 15 done
bat /var/log/system_utilization.log
Thu Mar 9 07:09:13 PM MSK 2023 CPU: 43.2% Mem: 32.1429% Disk:70% Thu Mar 9 07:09:29 PM MSK 2023 CPU: 45.7% Mem: 32.1429% Disk:70% Thu Mar 9 07:09:44 PM MSK 2023 CPU: 34.1% Mem: 32.1429% Disk:70% Thu Mar 9 07:09:59 PM MSK 2023 CPU: 28.5% Mem: 32.1429% Disk:70% Thu Mar 9 07:10:14 PM MSK 2023 CPU: 22.5% Mem: 32.1429% Disk:70% Thu Mar 9 07:10:30 PM MSK 2023 CPU: 34.8% Mem: 32.1429% Disk:70% Thu Mar 9 07:10:45 PM MSK 2023 CPU: 37.6% Mem: 32.1429% Disk:70% Thu Mar 9 07:11:00 PM MSK 2023 CPU: 29.8% Mem: 32.1429% Disk:70%