Ps ax with grep in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ps ax with grep in loop
# 1  
Old 07-10-2018
Ps ax with grep in loop

Hello,

I have built the following script to check if processes supplied by the argument are running or not.

Code:
#!/bin/bash

PROCLIST=$1

PROCESS="0"
ERROR_PROCS=""
IFS='+'
read -ra ADDR <<< "$PROCLIST"
for PROC in "${ADDR[@]}"; do
                if [ `ps ax | grep $PROC | grep -v grep | wc -l` -lt 1 ]; then
                PROCESS=1
                ERROR_PROCS="$ERROR_PROCS""$PROC ";
                fi
        done

        if [ $PROCESS -eq 1 ]; then
                echo "CRITICAL - One or more processes ($ERROR_PROCS) not running"
                exit 2
        fi


echo "OK - All monitored processes are running. Process: $PROCLIST"
exit 0

it seems it works fine apart from the fact the "ps ax | grep "process" | wc -l" gives a higher count than expected.
For example, if we take the process named "test" (which doesn't exist), it returns a count of 2.

Code:
[root@matt Linux]# ps ax | grep test | grep -v grep
[root@matt Linux]#

if We run the script with verbose:

Code:
[root@matt Linux]# bash -x ./test2.sh test
+ PROCLIST=test
+ PROCESS=0
+ ERROR_PROCS=
+ IFS=+
+ read -ra ADDR
+ for PROC in '"${ADDR[@]}"'
++ ps ax
++ grep test
++ wc -l
++ grep -v grep
+ '[' 2 -lt 1 ']'
+ '[' 0 -eq 1 ']'
+ echo 'OK - All monitored processes are running. Process: test'
OK - All monitored processes are running. Process: test
+ exit 0

What could be the reason that it captures the value of 2? This also happens if the process does exist. in that case, it returns a 3.

Rgds,

Matthew
# 2  
Old 07-10-2018
Did you consider "false positives"? Processes with the search string as part of the command (fittest, hottest, testcase)? E.g. grep man would show mman, manager on my system.



Any "test" user on the system?
# 3  
Old 07-10-2018
Hi RudiC,

In this case, the test was just used as an example. However, if we still take it for our case then nothing exists as a false positive:

Code:
[root@matt Linux]# ps ax | grep test | grep -v grep
[root@matt Linux]#

in that case that would have returned a value, which means there is nothing related to "test" in our list.

Also if you take an existing process, it will count but adds 2.

Here's another example:

process: master

Code:
[root@matt Linux]# ps ax | grep master | grep -v grep
 1705 ?        Ss     5:42 /usr/libexec/postfix/master -w

So one process exists

if we run the script I have returned a value of 3, 1 for the process 2 not sure why the bash script is originating this number.
I could eventually subtract the value but doesn't make sense.

Code:
[root@am1-stp-oam01 Linux]# bash -x ./test2.sh master
+ PROCLIST=master
+ PROCESS=0
+ ERROR_PROCS=
+ IFS=+
+ read -ra ADDR
+ for PROC in '"${ADDR[@]}"'
++ ps ax
++ grep -v grep
++ wc -l
++ grep master
+ '[' 3 -lt 1 ']'
+ '[' 0 -eq 1 ']'
+ echo 'OK - All monitored processes are running. Process: master'
OK - All monitored processes are running. Process: master
+ exit 0

# 4  
Old 07-10-2018
Strange. I vaguely remember we had a similar problem quite some time ago, but can't find the solution.
For debugging, in the script, echo the variables, and run the ps ax | ... pipe on its own to see its result.


Why, BTW, is above that complicated?
Code:
ps ax -ocomm= | grep -E "${1//+/|}" | sort | comm -13 - <(echo "${1//+/$'\n'}" | sort)

will serve you the non-running processes (of the plus-sign separated list in $1) on a silver plate...
This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-10-2018
Excellent, that really simplifies the job. I have used the one-liner for my benefit as follows:

Code:
#!/bin/bash

PROCLIST=$1
tmp=`ps ax -ocomm= | grep -E "${PROCLIST//+/|}" | sort | comm -13 - <(echo "${PROCLIST//+/$'\n'}" | sort) | wc -l`
tmp2=`ps ax -ocomm= | grep -E "${PROCLIST//+/|}" | sort | comm -13 - <(echo "${PROCLIST//+/$'\n'}" | sort) | tr "\n" " "`

if [ $tmp -gt 0 ]; then
        echo "CRITICAL - One or more processes (`echo $tmp2`) not running"
        else
        echo "OK - All monitored processes are running. Process: `echo $PROCLIST | sed 's/+/,/g'`"
        fi

tmp2 is just to produce them in a one-liner

Thanks again for your feedback
# 6  
Old 07-10-2018
Yes,
Code:
ps ax -ocomm=

does not list users or command arguments, so cannot unwantedly show false positives in them.
Even simpler than
Code:
ps ax -ocomm= | grep "$PROC"

is
Code:
pgrep "$PROC"

Both provide an exit status, so you don't need to count them (with -c option) and compare them.
Code:
if ps ax -ocomm= | grep -q "$PROC"; then echo running; fi

Code:
if pgrep "$PROC" >/dev/null; then echo running; fi

--
Last but not least, you can set IFS='+' temporarily for the read command:
Code:
IFS='+' read -ra ADDR <<< "$PROCLIST"
# continue with the original IFS

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 07-10-2018
Why that complicated?
Code:
TMP=$(ps ax -ocomm= | grep -E "${1//+/|}" | sort | comm -13 - <(echo "${1//+/$'\n'}" | sort))
[ $TMP ] && echo "CRITICAL - One or more processes ($TMP) not running" || echo "OK - All monitored processes are running. Process: ${1//+/,}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command in a loop

Hello - I am running a script that is outputting to a log. Let call it output.log I would like to monitor that log until the line "Build Successful" is found. I think I would need to use the grep command. How would I do that in a loop? Thanks Marty (1 Reply)
Discussion started by: MSpeare
1 Replies

2. Homework & Coursework Questions

GREP loop

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I need to search through the users home directories for keywords, display them. The code listed below will show... (7 Replies)
Discussion started by: jcllns1
7 Replies

3. Red Hat

using grep in a while loop

Hello everybody, I have been searching it, but it seems I am unable to find the correct information, that s why I am asking you guys, hoping somebody get an idea. Here is my problem : I want a script to loop until a string is identified in a log file. Here is the script : #!/bin/sh... (5 Replies)
Discussion started by: guyiom
5 Replies

4. Shell Programming and Scripting

Help on grep in a do while loop

So this is what I'm trying to do: I have a file called registry.txt which has a list of registry entries I want to search for. I have another file called inctrl.txt on which I want to perform the search on. Here's the example contents of registry.txt SOFTWARE\Microsoft\Security... (3 Replies)
Discussion started by: r4v3n
3 Replies

5. UNIX for Dummies Questions & Answers

grep sed and a loop

:wall: I have a requirement to search a log file that never rotates for certain values. If I find them I pipe them to a another file. To log file is constanyl being appened with new lines and never rotating Easy so far. The problem is I dont want to pipe out matches already seen before. ... (3 Replies)
Discussion started by: gunnahafta
3 Replies

6. Shell Programming and Scripting

Using grep within a while loop

Hi all, I have the below script to get input but i cannot get grep to work. input1.txt AAAAAAAAG input2.txt >gi|184009.1| LEAFY-like |AAAAAAAAGSGGGDHLPY However, when i use grep -f input1.txt input2.txt i cannot get any output matches (note that the match is underlined). Is it... (8 Replies)
Discussion started by: turkishvan
8 Replies

7. Shell Programming and Scripting

Help with grep inside an if loop

Hello All, I have been reading posts on here for a while, but this is my first post. I have a document in which many sentences appear, and I am piping it through an exterior script which will tag each word in the document with its part of speech (not part of my script, just background). The... (3 Replies)
Discussion started by: daf189
3 Replies

8. Shell Programming and Scripting

Grep commands in loop

Hi All, Reference to my previous post I need to compare all the lines in the file1 with file2 for this condition if file1 {$3,$5} ==file2 {$3,$5} then grep file2{$1}latest date. need output in file3 10/04/2008 09/04/2008 09/04/2008 08/04/2008 can anyone suggest me Thanks... (0 Replies)
Discussion started by: karthikn7974
0 Replies

9. UNIX for Dummies Questions & Answers

grep -v while loop

alist contain: a b c d e blist contain: a b c the code: #!/usr/bin/ksh cat blist | while read line do grep -V "$line" alist > data done (8 Replies)
Discussion started by: bobo
8 Replies

10. Shell Programming and Scripting

grep in a loop

Hi , I am trying a script which takes user input userid . I am stuck how to check whether that is a valid user id or not in the audit log files. My code is : cd $CCP_AUDIT cat * > /export/home/$USR/l***/files echo "UserId:\c" read UserId #Date Function echo "DATE : \c" read xxx I... (7 Replies)
Discussion started by: gundu
7 Replies
Login or Register to Ask a Question