How to monitor command output periodically
- Access to the Linux command line
When access to the command line is achieved, you will need to have a command you wish to monitor. This can be any Linux command or executable. For example, if you want to check a file’s contents, you could use:
watch -n 12 --differences=cumulative “cat /folder/file.txt | grep status”
The previous command will monitor the file.txt’s output fullscreen. Any tasks that need to be performed in parallel can be in a separate shell. The
watch command is monitoring lines in the file that contain the word “status.” The cat command’s output is piped to
grep so lines that contain “status” are matched. When multiple commands are used, it is necessary to enclose them in quotes.
watch command above is executing the commands in quotes every 12 seconds, specified by the
-n argument. Differences in output every 12 seconds can be highlighted using the
-d argument. If you prefer for the highlighting to remain in between
watch refreshes, you can use:
--differences=cumulative. This may be necessary if highlighting will be lost before it can be inspected or if the output refresh rate is too fast.
man watchfrom the Linux command line.