watch what `watch` can do
I’ve been running a lot of scripts recently that require me to check on things that I can’t monitor with `tail -f logname`. Mainly to keep an eye on a process and get information about the processes running that I’m interested in. Periodically, one hangs and I like to just jump back to it and kill it.
Watch to the rescue!
Watch executes a command at a specified period. For instance, if I’m extracting a file and want to monitor the progress, I can simply type:
watch 'ls -la filename.ext'
and I will get the output from `ls -la filename.ext` every 2 seconds.
If my internet connection is down and I am impatiently waiting for it to come back up, I can run:
watch -n15 'ping -c1 www.google.com'
If I’m forking processes in a Bash script and want to make sure none of them are hung:
watch -n2 'ps ax|grep [p]rocessname'
It’s very handy!
I periodically come across an issue whereby `tail -f /var/log/syslog` will hang after operating for a couple of hours. I solve this by using the following instead:
watch -n5 'tail -n20 /var/log/syslog'
`man watch` to find more uses for this program. I tried `woman watch` but apparently Linux doesn’t carry those.
and holy sweet crap, it’s been a long time since I wrote an article on this site!
Until next time,
-LightningCrash
