Ps ax with grep in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ps ax with grep in loop
# 8  
Old 07-10-2018
The original command that you've shown us that you're using to see if master was running is:
Code:
[root@am1-stp-oam01 Linux]# bash -x ./test2.sh master

So, if master is running, you get a count of 1 for the process you're looking for, an additional 1 because bash is running ./test2.sh master, and a third 1 because you're running bash -x ./test2.sh master.

Sometimes it is easier to debug things like this by changing:
Code:
[ `ps ax | grep $PROC | grep -v grep | wc -l` -lt 1 ]

to:
Code:
[ `ps ax | tee step1 | grep $PROC | tee step2 | grep -v grep | tee step3 | wc -l` -lt 1 ]

and examine the contents of the files step1, step2, and step3 to see what processes were matched that you hadn't expected.

As RudiC suggested, using ps -ax -ocomm gets rid of the problem here. But adding tees in a pipeline frequently helps when shortcuts like -o comm don't apply.
These 3 Users Gave Thanks to Don Cragun For This Post:
# 9  
Old 07-11-2018
Quote:
Originally Posted by RudiC
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//+/,}"

The same idea but implemented with two grep's
Code:
#!/bin/bash
plist=${1//+/$'\n'}
TMP=$(fgrep -vxf <(ps -eo comm= | fgrep -x "$plist") <<< "$plist")
if [ -n "$TMP" ]
then
  echo "processes not running:"
  echo "$TMP"
else
  echo "Ok"
fi

Note that [ $TMP ] is not robust in case $TMP contains shell-special characters or test-operators like -n or =
So should be quoted and prefixed with a -n operator.
[[ $TMP ]] might be safe as well.
# 10  
Old 07-11-2018
if pgrep isn't what you want, could you make an expression from the item you are searching for? I avoid using a contruct like ps -ef | grep this | grep -v grep byt writing it as ps -ef | grep -E "thi[s]" so the expression does not match its own process. If you are passing it a loop of items to check, it could get a bit fiddly, but with variable substitution you could achieve it, perhaps like this:-
Code:
set -x

for PROC in "${ADDR[@]}"; do
do
   PROC_a="${PROC%?}"                           # Chop off last character
   PROC_b="${PROC#$PROC_a}"                     # Work out the last character
   PROC_E="${PROC_a}[${PROC_b}]"                # Assemble expression
   if ! $(ps ax | grep -Eq "$PROC_E")           # Test for a non-zero return code when looking for processes
      PROCESS=1  
      ERROR_PROCS="${ERROR_PROCS} ${PROC}"              
   fi           
done

echo "Failed to find ${ERROR_PROCS}"
set +x

You still might have to be careful because there is a risk that there are false positives, e.g. someone stops a service called MAINPROC (so there are no process like that running, but then edits the file /var/log/MAINPROC, and the editor command shows up as a process matching your search and therefore you think it is still running.

Can you tell us more about the processes you are looking for and therefore might be a better way to be checking for them. Perhaps if they write their process-id in a file in /var/run/name then you can read that file and make sure the process is what it should be.

It depends how far you want to push this. You processes might respond to a signal to say that they are running okay, for instance and you could actually give them a nudge to make sure that they are happy and not stuck in a loop, for instance or they could frequently be re-writing a file with the current date (best as date +%s format) and if it is out of date by too long (you decide what is too long and compare to current date +%s value) then raise an alert.

There are many ways to do it.

Robin
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