brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: watch -g waits until the output of a command changes and then stops

Linux
~/tips/tip-linux-watch-g-exit-on-change

watch -g -n 5 ls /srv/inbox && echo 'new file in the inbox'

Wait for a file, a service or a disk to change without writing a loop.

Waiting for something to change, a file that arrives or a service that comes up, usually means a small loop with sleep. The procps watch has an option that does the waiting for you.

watch -g -n 5 ls /srv/inbox && echo 'new file in the inbox'

-n 5 runs ls every five seconds and shows the output full screen, as usual. -g makes watch exit as soon as that output differs from the previous run. Whatever comes after && then runs, so you can chain a sound, a notification or the next step of your work.

Some other uses I like:

watch -g systemctl is-active nginx
watch -g -n 60 'df -h /mnt/backup'

The first stops when nginx goes from active to something else, or back. The second stops the moment the free space on the backup disk changes, so you know a copy started or finished.

Pick the command with care. Any change ends the wait, including a clock, a counter or a sorting difference. If the output includes a timestamp, watch -g stops after the first interval. Filter the output down to the part you care about with grep or cut.

The related -e option stops on a non zero exit code instead, which helps when you want to watch until a command fails.

Further reading: watch(1) manual page

Going deeper: Keep a domain pointed at your home with Cloudflare DDNS on Unraid

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link