How To Repeat a Command at Regular Intervals in Terminal on Linux
Monitoring system resources or specific outputs in real-time can be really helpful — especially when troubleshooting or just trying to see what’s happening behind the scenes. The watch
command in Ubuntu is surprisingly handy for this. It repeats any command at intervals you define, so you don’t have to keep re-typing it or constantly refresh your terminal. But sometimes it trips people up because it’s a pretty simple tool, yet misused or misunderstood. This guide tries to clear that up and give some practical tips to get the most out of it. After following along, you’ll be able to keep an eye on memory, CPU, disk space, or pretty much anything else in real time without breaking a sweat.
How to Fix Common Issues When Using the Watch Command in Ubuntu
Watch command not working or output not updating as expected
If the watch
command isn’t refreshing, or it’s giving errors, the first thing to check is whether the command you’re passing works fine on its own. Sometimes a command, like free -h
, might be fine when run alone but causes issues via watch
. Or maybe you’re missing the -n
parameter, so it defaults to a long interval, making it seem like it’s not updating. Also, make sure you’re running the command with correct syntax, like watch -n 2 free -h
(which refreshes every 2 seconds). Check if your terminal window supports frequent updates — some remote SSH sessions can have refresh issues or lag.
Side note: on some setups, watch
refuses to run certain commands if permissions are off or if the command outputs are too verbose. In that case, trying to run the command separately first helps to troubleshoot. Also, if the output looks frozen, hitting Ctrl +
What to do if the refresh rate is too slow or too fast
Adjusting how often the command runs is key. Use -n
followed by seconds, like watch -n 1 command
for every second, or watch -n 5 command
for every five. This can help if it’s too jittery or if you don’t need frequent updates. Just remember, setting really short intervals (like below 1 second) can cause high CPU load or flood your terminal with data. Not sure why it works, but sometimes a quick reboot of your terminal session or SSH connection improves things — weird, but true.
Fix 1: Making sure the command is correct and runs on its own
Sometimes the problem is just that the command isn’t working outside watch
. For example, if you’re trying to monitor disk space with df -h
, run that alone first: df -h
. If it throws errors or doesn’t output anything meaningful, fix that first. Once it works standalone, wrapping it with watch -n 2 df -h
should give real-time updates. Also, make sure the command is available in your PATH — for example, some commands like htop
or top
don’t need watch
to be monitored, but they often do better directly in their own interfaces.
Fix 2: Adjusting the command options or environment
If your command is verbose or produces too much data, consider refining it. For instance, for memory, instead of free -m
, try free -m | grep Mem
to focus on the main info and keep the output cleaner. Also, check your terminal’s scrollback buffer; sometimes, if it’s full or small, the refresh can look weird or laggy. Increasing your terminal’s scrollback limit or resizing the window might help.
Fix 3: Using alternative tools or commands for specific tasks
Sometimes watch
isn’t the best fit, especially for high-frequency updates or complex outputs. For example, for CPU load, you might prefer htop, which is an interactive process viewer. For disk space, hitting Settings > Disk Usage Analyzer could be more visual. But if command-line monitoring is preferred, combining watch
with targeted commands like vmstat
, top
, or other system commands can be very effective.
Extra tip: Keep in mind that some commands may require sudo
If you’re trying to watch something that needs root privileges, like disk info on certain partitions, run sudo watch -n 2 df -h
. Just be cautious — running sudo
every time might be overkill, but sometimes it’s the only way to get accurate data.
Wrap-up
Using watch
in Ubuntu isn’t complicated, but it can be a little tricky to troubleshoot if things aren’t updating or commands fail. Checking that the commands work solo, tweaking your interval, and making sure your terminal can handle continuous output usually clears up the issues. On some setups, just restarting your terminal or SSH session fixes weird glitches. Play around with different commands and options to see what gives the best real-time info for your needs.
Summary
- Test commands separately to ensure they work.
- Use
-n
to set update interval, likewatch -n 2
. - Adjust your terminal settings if output is laggy.
- Consider alternative tools like htop for process monitoring.
- Run with
sudo
if permission issues crop up.