Linux administrators and power users often need to run commands repeatedly. Whether you’re monitoring system performance, tracking log changes, or automating routine tasks, the ability to repeat a command at a specified interval is essential.
While cron has been a traditional tool, its minimum resolution is one minute—which can be too slow for real-time applications. In this guide, we explore multiple techniques to repeat commands every X seconds using built-in utilities like watch, flexible shell loops with sleep, built-in repeat commands in some shells, and modern scheduling with systemd timers. We also cover advanced drift compensation techniques to ensure precise intervals.
watch
CommandThe watch command is a simple and powerful utility that executes a given command at fixed intervals. It clears the terminal screen and updates the output, making it ideal for real-time monitoring.
To run a command every X seconds, use:
watch -n X command
>
Example: Monitor free memory every 5 seconds:
watch -n 5 free -m
-d
flag to highlight changes between updates: watch -d -n 5 free -m
-t
option to remove the header (showing the interval, command, and current time): watch -t -n 5 free -m
watch -n 5 'ps aux | grep firefox'
watch
watch -n 2 'tail -n 20 /var/log/syslog'
Note: ‘watch’ clears the screen each time it updates. A shell loop might be a better option if you need a persistent history.
Read: Task scheduling on Linux: CRONTAB
sleep
Shell loops provide flexibility, especially when you want to log outputs, run multiple commands, or avoid screen clearing. They are written using simple Bash scripting.
A simple loop to run a command every X seconds:
while true; do
command
>
sleep X
done
Example: Display the current date every 5 seconds:
while true; do
date
sleep 5
done
You can condense the loop into a one-liner:
while true; do date; sleep 5; done
Or place sleep in the condition:
while sleep 5; do date; done
This version waits 5 seconds before the first command execution.
Read: How to tweet from the Linux Command Line
If you want the command to run a specific number of times:
for i in {1..10}; do
echo "Iteration $i: $(date)"
sleep 5
done
When the command takes noticeable time to run, the overall cycle can drift. To adjust for this, calculate the command’s duration:
while true; do
start=$(date +%s)
duration=$(($(date +%s) - start))
sleep_time=$(( X - duration ))
[ $sleep_time -gt 0 ] && sleep $sleep_time
done
Example: Repeat a command every 5 seconds:
while true; do
start=$(date +%s)
echo "Current time:" $(date)
duration=$(($(date +%s) - start))
sleep_time=$(( 5 - duration ))
[ $sleep_time -gt 0 ] && sleep $sleep_time
done
This method helps maintain a consistent interval by compensating for the command’s run time.
repeat
Command in Certain ShellsSome shells like csh, tcsh, and zsh offer a built-in repeat command. This command is not available in Bash, but if you use one of these shells, you can leverage it.
To repeat a command 5 times:
repeat 5 echo "Hello, world!"
To introduce a delay, combine it with a loop:
foreach i (`seq 1 5`)
echo "Hello, world!"
sleep 2
end
This method is shell-specific and useful if you prefer these environments.
Read: How to use systemd to troubleshoot Linux problems
For robust scheduling that persists across reboots and offers finer control than cron, systemd timers are an excellent choice. They use dedicated unit files to define when and how tasks should run.
A systemd timer consists of two unit files:
Monotonic timers use relative time (e.g., after boot or after the last execution).
Create /etc/systemd/system/example.timer
:
[Unit]
Description=Run example task 10 minutes after boot
[Timer]
OnBootSec=10min
Unit=example.service
[Install]
WantedBy=timers.target
And /etc/systemd/system/example.service
:
[Unit]
Description=Example Service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/example-script.sh
Activate it with:
sudo systemctl enable --now example.timer
Read: How to Manage Ubuntu Boot Services: List, Start, and Stop Systemd Services at Startup
Realtime timers use calendar scheduling with the OnCalendar keyword.
Create /etc/systemd/system/daily-task.timer
:
[Unit]
Description=Daily task at midnight
[Timer]
OnCalendar=daily
Persistent=true
Unit=daily-task.service
[Install]
WantedBy=timers.target
And /etc/systemd/system/daily-task.service
:
[Unit]
Description=Daily Task Service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/daily-script.sh
Enable with:
sudo systemctl enable --now daily-task.timer
The Persistent=true option ensures that missed tasks (e.g., when the system is off) are executed immediately upon reboot.
For temporary scheduling without creating persistent files, use systemd-run:
systemd-run --user --on-calendar '*:0/1' /bin/sh -c "date >> ~/log.txt"
This command creates temporary timer and service units that run every minute. Stop them using:
systemctl --user stop .timer
To list active timers:
systemctl list-timers
Inspect a timer’s configuration:
systemctl cat daily-task.timer
Stop a timer:
sudo systemctl stop daily-task.timer
Read: How to clear systemd journal Logs
Anacron is ideal for desktop or laptop systems that are not always on. Unlike cron, which runs as a daemon on continuously running systems, anacron runs tasks that should occur at least once per day, even if the system was off during the scheduled time.
Anacron reads a configuration file called anacrontab, which specifies tasks, their frequency (in days), and a delay before running. It is designed for systems that do not run 24/7.
On Debian-based systems:
On Fedora or RHEL-based systems:
On Arch Linux, you might need to install cronie as anacron is not installed by default.
The anacrontab file is usually located at /etc/anacrontab. Its structure is as follows:
Example anacrontab entry:
This runs the daily cron jobs with a delay of 5 minutes.
For desktops or laptops, you can set up a per-user anacron:
Create a local anacrontab:
Edit your local anacrontab as needed (e.g., change MAILTO or add custom jobs).
Schedule anacron to run hourly by adding this line to your personal crontab (crontab -e
):
For scenarios where even a small timing drift is unacceptable, you can build advanced loops that calculate the elapsed time using Unix timestamps. For example:
PERIOD=5
while true; do
start=$(date +%s)
command
>
duration=$(($(date +%s) - start))
sleep_time=$(( PERIOD - duration ))
if [ $sleep_time -gt 0 ]; then
sleep $sleep_time
else
echo "Warning: Command took longer than the period of $PERIOD seconds!"
fi
done
This script ensures that the command runs as close as possible to every 5 seconds and warns if the command execution overruns the desired interval.
Repeating Linux commands at specific intervals is a versatile skill that enhances real-time monitoring and task automation. Whether you choose the simplicity of the watch command, the flexibility of shell loops with sleep, the built-in repeat functions of certain shells, or the robust scheduling capabilities of systemd timers, or anacron, you now have multiple methods to suit any scenario.
By understanding these techniques and their use cases, you can ensure that your system management tasks run exactly when needed—whether for troubleshooting, performance monitoring, or automating routine processes. Experiment with these methods and tailor them to your environment for optimal results.
The post Repeat Linux Commands Every X Seconds for Real-Time Monitoring, Automation & Precise Scheduling appeared first on net2.
Data centers are popping up everywhere. With the rapid growth of AI, cloud services, streaming…
Our commitment to building a thriving open source community is stronger than ever. We believe…
The clock was ticking: Node.js 18’s upstream End of Life (EOL) The OpenJS Foundation is…
June 25th, 2025 – Canonical, the company behind Ubuntu, and Pure Storage, the IT pioneer…
Co-authored with Julie Muzina A year ago, during our Madrid Engineering Sprint, we challenged ourselves…
Welcome to the Ubuntu Weekly Newsletter, Issue 897 for the week of June 15 –…
View Comments