Article 5KBSR [SOLVED] process monitor code - string comparison errors

[SOLVED] process monitor code - string comparison errors

by
linxbee
from LinuxQuestions.org on (#5KBSR)
My System details are :
NAME="Red Hat Enterprise Linux Server"
VERSION="7.2 (Maipo)"
Shell = bash

I am trying to build a process monitor, that prints the status of the process.

In the below code , I am trying to compare the process state pid_status with strings we get from command /proc/$pid/status

But it throws error at the comparison if ["$pid_status" =~ .*"sleeping"*. ] || ["$pid_status" == *"stopped"* ] || ["$pid_status" == *"running"* ];

The error is:

Quote:
./process_monitor_lq.sh: line 29: $'[State:\tS (sleeping)': command not found
I have tried to do other alternatives by removing tabs from command output and also extracting part of strings, but none succeeded.

Following is the output I am getting:

Quote:

The pid is 31395
Process with 31395 is monitored for its status
*Status = State: S (sleeping)
./process_monitor_lq.sh: line 22: pid_status1: command not found
#Status =
./process_monitor_lq.sh: line 25: x: command not found
./process_monitor_lq.sh: line 26: y: command not found
x =
y =
./process_monitor_lq.sh: line 29: $'[State:\tS (sleeping)': command not found
./process_monitor_lq.sh: line 29: $'[State:\tS (sleeping)': command not found
./process_monitor_lq.sh: line 29: $'[State:\tS (sleeping)': command not found
The pid is
pid entry is not available in the /proc
The pid is
pid entry is not available in the /proc
The pid is 31395
Process with 31395 is monitored for its status
*Status = State: R (running)
./process_monitor_lq.sh: line 22: pid_status1: command not found
#Status =
./process_monitor_lq.sh: line 25: x: command not found
./process_monitor_lq.sh: line 26: y: command not found
x =
y =
./process_monitor_lq.sh: line 29: $'[State:\tR (running)': command not found
./process_monitor_lq.sh: line 29: $'[State:\tR (running)': command not found
./process_monitor_lq.sh: line 29: $'[State:\tR (running)': command not found

Code:
#!/bin/bash
SUB_S = "sleeping"
SUB_R = "running"

do_start() {
# List of process names to be monitored for its status(R,S,T etc).
declare -a PROCESS_LIST
PROCESS_LIST=("process_1" "process_2" "process_3" )

for process in "${PROCESS_LIST[@]}"; do
pid=$(pidof $process)
echo "The pid is $pid"
if [ $pid ]; then
#Do Nothing
echo " Process with $pid is monitored for its status"

pid_status=`head /proc/$pid/status | grep "State:*"`
echo "*Status = $pid_status"
#trying to remove spaces and tabs, but does not work
#pid_status = echo "$pid_status" | sed -e 's/^[[:space:]]*//'
#pid_status1 = echo "$pid_status" | sed -E 's,\\t|\\r|\\n,,g'
pid_status1 = echo "$pid_status" | tr -d '[:space:]'
echo "#Status = $pid_status1"
#trying to extract part of status, but does not work
x = grep -q "$SUB_R" <<< "$pid_status"
y = grep -q "$SUB_S" <<< "$pid_status"
echo "x = $x"
echo "y = $y"
if ["$pid_status" =~ .*"sleeping"*. ] || ["$pid_status" == *"stopped"* ] || ["$pid_status" == *"running"* ]; then
echo "process:$process with pid $pid is having status $pid_status"
fi
else
echo "pid entry is not available in the /proc"
fi
done
}

while :
do
do_start
sleep 2
doneRequest any experts help, TIAlatest?d=yIl2AUoC8zA latest?i=h-TRtQISdp8:OS4mzGCdolY:F7zBnMy latest?i=h-TRtQISdp8:OS4mzGCdolY:V_sGLiP latest?d=qj6IDK7rITs latest?i=h-TRtQISdp8:OS4mzGCdolY:gIN9vFwh-TRtQISdp8
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments