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

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
  • 5,213
MShaffer
  • 146