1

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]<defunct>   
1
  • Disclaimer: I have not read the entire question too deeply. That said... Some zombie processes may be necessary; A parent process may have not yet reaped a zombied child process so it can inspect the result/return code. The parent may malfunction if you terminate/kill it and the parent later tries to reap it. If you are having trouble with too many unreaped zombies, you should look in to why, and which parent process is running around trying to kickstart a virtual Zombie Apocalypse--and fix that.
    – C. M.
    Jun 24 at 13:13
1

That's not related to the zombie status but to the fact that pidof returns the pids of all processes running the named program. The child of your zombie program is also called zombie, so both pids are printed.

pidof has an option -s which tells it to return only one pid. Alternatively, you could enumerate the pids returned and check each in turn.

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.