Sample Header Ad - 728x90

handling zombie process status

1 vote
1 answer
448 views
I am new to scripting. I am writing a process status monitor script that should catch when a process gets killed, zombied or sleeping etc. I am not sure this is really a good script, if any better alternatives are there pls suggest. For the problem I am facing, below script can catch the status running, sleeping, stopped etc. But for the zombie process, we see two pids for the command pidof zombie. which is getting treated as error and the script does not catch that status. Can anyone suggest how to catch the zombie state of the process. By the way, I have created a zombie process whose status can be seen as zombie for sometime on the shell.
#!/bin/bash

do_start() {
    # List of process names to be monitored for its termination/killing.
    declare -a PROCESS_LIST
    PROCESS_LIST=("process_1" "process_2" "zombie")
    for process in "${PROCESS_LIST[@]}"; do
        echo "checking for $process"
        pid=$(pidof $process)
        echo "its pid is $pid"
        if [ $pid ]; then
            pid_status=head /proc/$pid/status | grep "State:*"
            echo $pid_status
            if [[ "$pid_status" =~ .*"sleeping"*. ]] || [[ "$pid_status" =~ .*"stopped"*. ]] || [[ "$pid_status" =~ .*"running"*. ]]; then
                echo "process:$process with pid $pid is having status $pid_status"
            elif [[ "$pid_status" =~ .*"zombie"*. ]]; then
                echo "process:$process with pid $pid is having status $pid_status"
            fi
        else
            echo "pid not present" > /dev/null
        fi
    done
}

while :
do
    do_start
    sleep 2
done
__OUTPUT__: >checking for process_1 its pid is 12668 State: S (sleeping) process:process_1 with pid 12668 is having status State: S (sleeping) checking for zombie its pid is 12818 12817 ./process_monitor.sh: line 13: [: 12818: unary operator expected > checking for process_1 its pid is 12668 State: R (running) process:process_1 with pid 12668 is having status State: R (running) checking for zombie its pid is 12818 12817 ./process_monitor.sh: line 13: [: 12818: unary operator expected __$ ps ux output__
xolo    12668 95.3  0.0   4164   360 pts/4    R    15:32   0:07 ./process_1  
xolo    12817  0.0  0.0   4160   360 pts/4    S    15:32   0:00 ./zombie  
xolo    12818  0.0  0.0      0     0 pts/4    Z    15:32   0:00 [zombie]
Asked by IrAM (133 rep)
Jun 24, 2021, 10:18 AM
Last activity: Jun 24, 2021, 11:55 AM