0

I stumbled across this link: "watch" the output of a command until a particular string is observed and then exit

I know the PID of the process. I want to log the process "resource-consumption" using top.

Basically top -p PID -b >> LOGFILE.

I want to have this run in increments, say every 5 seconds using the "watch" command

watch -n 5

From an independent/external program, I will append "We Are Finished" to the logfile.

I want the logging of watch/top to break/exit when "We are Finished" So grep should be evaluating the entire log file, not just the current value.

I want the command to run in a nonblocking form... maybe ends with " & "

I set -d to 5 so top and watch are creating at the same time? ...

Here is my attempt using the "-e" option suggested from the link above, not working as expected... PID and LOGFILE are appropriate values.

watch -n 5 -e "!  top -d 5 -b -p PID >> LOGFILE | grep -m 1 \"We Are Finished\"" &
mshaffer
  • 103
  • 2
  • If you append standard output to `LOGFILE`, then nothing is piped to `grep`. You need to pipe through `tee -a LOGFILE` to direct output to both places, or (more complex) make `grep` take its input from `LOGFILE` after it has been written. – AFH Jan 26 '17 at 16:05
  • @afh thanks for the ideas... can you provide the tee syntax? I do not need "more complex" since "We are Finished" is not being written by this watch loop. – mshaffer Jan 26 '17 at 16:11
  • watch -n 5 -e "! top -d 5 -b -p PID >> LOGFILE | grep -m 1 \"We Are Finished\" LOGFILE " & – mshaffer Jan 26 '17 at 16:12
  • I don't understand your comments: you have just reiterated your original command with an erroneous extra double-quote. I gave you the syntax in my comment above: replace `>> LOGFILE` with `| tee -a LOGFILE`, but if `"We are Finished"` is not being written why are you looking for it? – AFH Jan 26 '17 at 16:26
  • @AFH "We are Finished" is being written by an external program... The external program executes this "logging" feature.... and the logging feature stops itself on "We are Finished" – mshaffer Jan 26 '17 at 16:30
  • This is why I added "LOGFILE" twice. once on the grep side... – mshaffer Jan 26 '17 at 16:31
  • 1
    Sorry, I didn't notice it on the end. You should use `&&` instead of `|` before `grep`. The reason I said this was more complex is that `"We are Finished"` may be in `LOGFILE` before the command is run, whereas `tee` detects the string in each run of the command only. – AFH Jan 26 '17 at 16:45

1 Answers1

0
until grep -qm 1 '\"We Are Finished\"' LOGFILE; do top -d 5 -n 12 -b -p PID >> LOGFILE; done

If you want to exit the logging on a process when the pid is no longer running the script below should work? (this avoids two processes writing to same file?)

It takes three parameters: the PID, the location of LOGFILE and the TIME between log entries.


#!/bin/bash

if [ $# -lt 3 ]
  then
    echo "Usage logpid (PID) (LOGFILE) (Time between in sec)"
    exit 1;
fi

while :
    do 
    if  ps -p $1 > /dev/null ; then
    top -n 1 -b -p $1 >> $2;
    else
    echo "Exited No pid";
    break;
    fi
    sleep $3;   
done
Toby Speight
  • 4,866
  • 1
  • 26
  • 36
MShaffer
  • 146
  • 4