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\"" &
LOGFILE, then nothing is piped togrep. You need to pipe throughtee -a LOGFILEto direct output to both places, or (more complex) makegreptake its input fromLOGFILEafter it has been written. – AFH Jan 26 '17 at 16:05>> LOGFILEwith| 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&&instead of|beforegrep. The reason I said this was more complex is that"We are Finished"may be inLOGFILEbefore the command is run, whereasteedetects the string in each run of the command only. – AFH Jan 26 '17 at 16:45