LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   process monitor code - string comparison errors (https://www.linuxquestions.org/questions/linux-newbie-8/process-monitor-code-string-comparison-errors-4175696734/)

linxbee 06-21-2021 07:22 PM

process monitor code - string comparison errors
 
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
done

Request any experts help, TIA

pan64 06-22-2021 01:31 AM

you must add space before/after [ and ].
From the other hand you can try shellcheck to analyze your script. That will find more errors and also will suggest solutions.

linxbee 06-22-2021 01:43 AM

Thanks pan64 for the reply.

I new modified statement is:

Code:

if [ "$pid_status" =~ .*"sleeping"*. ] || [ "$pid_status" =~ .*"stopped"*. ] || [ "$pid_status" =~ .*"running"*. ]; then
The old error is gone , but getting a new error as below

Quote:

[: =~: binary operator expected

pan64 06-22-2021 02:01 AM

use double [[ and ]] instead of [ and ]. that may help.

linxbee 06-22-2021 02:09 AM

Thanks again pan64.
Its so simple , am not frequent user of shell script.

MadeInGermany 06-22-2021 02:15 AM

The =~ operator only works for the [[ ]] compound, not the [ command.
Its right hand side is an extended regular expression (ERE, like egrep or grep -E).
A .* means "any character any times" and is useless at the beginning or end of an ERE because it is "floating".

The == operator within a [[ ]] is a shell glob.
A * means "any character any times" and make sense at the beginning or end of the glob, because the glob is "anchored".

Code:

            if [[ "$pid_status" =~ "sleeping" ]] || [[ "$pid_status" == *"stopped"* ]] || [[ "$pid_status" == *"running"* ]]; then
The [ command is more expanded than the [[ ]] compound.
You better have
Code:

if [ "$pid" ]; then
or
Code:

if [ -n "$pid" ]; then
because $variables in command arguments should be quoted to avoid unwanted substitutions.
No quotes needed in
Code:

if [[ $pid] ]; then
or
Code:

if [[ -n $pid ]]; then


All times are GMT -5. The time now is 06:47 AM.