3

I'm using watch -g to monitor the output of a command and exit as soon as it changes. The problem is that all the UI elements of watch (including the output of the command that I'm monitoring) disappear as soon as it exits. I want to know what the output changed to before watch exited. Is this possible?

Ryan
  • 31

2 Answers2

0
watch -g "ls | tee -a log_file"

In log_file you will get the output of your command. ls is just example, of course you will use your own command or if more of them you should put them in parentheses like in answer below. Credit to this answer.

For easier recognition of differences between last and second last output you could add empty new line and some markation:

watch -g "(echo; echo 'New output below:'; echo '------------------'; ls) | tee -a log_file"
Damir
  • 501
0

If you're willing to not use watch, try this and it will show the most recent output when the command failed. Replace somecommand with your command.

logFile='/var/log/somecommand.log'
somecommand &> $logFile &
expectedOutput=$(cat $logFile)
while true; do
  somecommand &> $logFile &
  actualOutput=$(cat $logFile)
  if [[ "$actualOutput" != "$expectedOutput" ]]; then
    echo "Process failed, here is the most recent output..."
    echo "$actualOutput"
    exit 1
  fi
  sleep 2
done