LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2021, 07:22 PM   #1
linxbee
Member
 
Registered: Jan 2020
Distribution: RHEL,CENTOS, Ubuntu
Posts: 52

Rep: Reputation: Disabled
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

Last edited by linxbee; 06-21-2021 at 08:23 PM. Reason: quoted the output and error
 
Old 06-22-2021, 01:31 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
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.
 
1 members found this post helpful.
Old 06-22-2021, 01:43 AM   #3
linxbee
Member
 
Registered: Jan 2020
Distribution: RHEL,CENTOS, Ubuntu
Posts: 52

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-22-2021, 02:01 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
use double [[ and ]] instead of [ and ]. that may help.
 
1 members found this post helpful.
Old 06-22-2021, 02:09 AM   #5
linxbee
Member
 
Registered: Jan 2020
Distribution: RHEL,CENTOS, Ubuntu
Posts: 52

Original Poster
Rep: Reputation: Disabled
Thanks again pan64.
Its so simple , am not frequent user of shell script.
 
Old 06-22-2021, 02:15 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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

Last edited by MadeInGermany; 06-22-2021 at 02:16 AM.
 
  


Reply

Tags
shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
Errors, Errors, and more Errors (KDE 3.4.x GUI Errors) Dralnu Linux - Software 2 05-13-2006 08:30 AM
String Input & Array Comparison Problem azucarmom Programming 2 03-13-2005 07:23 AM
perl string comparison problem AM1SHFURN1TURE Programming 3 03-06-2005 10:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration