This is one of the things I wish I knew earlier. There have been a lot of times, where I needed to constantly monitor the output of a command and all I would do is use the up(arrow) and enter key to constantly run the previous command. If you, too, have been in a situation, I hope you do what is required in a more optimized manner after reading this post.
You can use this to execute a command repeatedly and monitor the output in full-screen mode. Suppose you want to constantly monitor the files inside a directory(e.g. '
/tmp/images'), you may use the ls command with
watch ls /tmp/images
By default, this watch runs the command every 2 seconds, in case you need to change that value, you can pass the required value to the n parameter.
watch -n 3 ls /tmp/images
Highlighting the difference
While monitoring, it is generally a pain to monitor the difference manually. This is where the
-d parameter comes to our rescue. You can use it to highlight the difference.
watch -d ls /tmp/images
A complex example
Now that we know what powers are bestowed to us by the
watch command, let us try to monitor a more complicated scenario. Consider a situation where you would want to monitor the state of all "python" process. The below command can help you do it.
watch -n 3 "ps aux | grep python"
The below screenshots try to show some part of the images when run on my machine.
In case, you are interested in hacking more into the
watch command, you can use the
-h flag to display all the options that you can use with it.
# for seeing the complete list of options watch -h
Till we meet in another post, keep hacking.