1 minute, 46 seconds
A quick post to spread the love about the watch
command – it’s amazing!
Prior to your knowledge of watch
, maybe you wanted to run a command every 2 seconds, like say hello
(credit to SO for this one):
$ while true
> do
> echo "hello"
> sleep 2
> done
But this is tedious and awkward. Maybe you collapse into the ol’ bash oneliner:
while true; do echo "hello";sleep 2;done
This is a small improvement, but A better way is with watch
!
watch echo "hello"
So much nicer, right?! Watch runs any command you follow it with ever 2 seconds – boom!
But the real power comes when your checking the progress of something over time. Maybe you want to see when the login for a website comes backup with a 200
? You’d of course reach for curl
, but it’s a pain to run over and over. Again, watch
makes short work of this:
watch 'curl -o /dev/null -s -w "%{http_code}\n" https://example.com/login'
But the real REAL REAL power, in my opinion, is when you want to do multiple things at a time. So instead of switching back and forth between terminals to do so, just chain them together with a bit of echo
for spacing. This makes it trivial to watch the load average, get the last few lines of a debug log and tail a docker container all in one spot like so:
DOCKER LOGS";docker logs some-container --since 10s;echo;echo "DEBUG CSV";tail --lines 2 debug.csv;echo;echo "UPTIME";uptime
This looks like this:
Every 2.0s: echo "DOCKER L... ip-172-31-27-227: Wed Oct 8 23:04:22 2025 DOCKER LOGS 2025-10-08T22:30:30.673 INFO: foo/bar [100%] 2025-10-08T22:30:30.673 INFO: foo/quux-client [54%] 2025-10-08T22:30:35.676 INFO: foo/foo [100%] 2025-10-08T22:30:35.676 INFO: foo/quux-client [55%] DEBUG CSV 1759962312,4.21.1,3.5.0,89844560,108220976 1759962433,4.21.1,3.5.0,92531624,108619512 UPTIME 22:30:36 up 15 days, 35 min, 3 users, load average: 14.13, 13.83, 16.16
The beauty of this Is that I started quickly with just one command running in watch
. Then I could iteratively add another one and anothe rone. The longer the job ran, the more I could add in, like the nice labels in echo
and such. I’m SSHed in to a remote server and I’m tmux`ed
up to my neck and in one of the panes, this watch
job dutifully runs, keeping “watch” on all things (see what I did there?).
The fact that watch
handles the refresh (configurable too!) and redraws the content in place without any flickering is just lovely! watch
on!!