We have all been there. You set up a cool automated workflow—maybe an image captioning AI on a remote GPU server, a sync script for backups, or a quick data fetcher—and suddenly you realize: “Wait, I need this thing to run continuously every couple of minutes.”
Just recently, I was working on a project where I needed to sync a local folder full of generated images to Google Drive. The command itself was simple using rclone:
Bash
rclone copy /workspace/runpod-slim/ComfyUI/output gdrive:SomeFolder/output/ --progress
Running it manually once is fine. Running it manually every 120 seconds while trying to wrangle four kids at home? Not happening.
If you just want this job done quickly, safely, and without unnecessary complexity, here are three real-world ways to loop a command every 2 minutes on Linux.
Method 1: The Quick-and-Dirty Shell Loop (Fastest)
If you are logged into your server via SSH and just want to kick off a quick loop while you work, a standard while loop in Bash is your best friend.
How to do it:
Open your terminal and run this:
Bash
while true; do
rclone copy /workspace/runpod-slim/ComfyUI/output gdrive:SomeFolder/output/ --progress
sleep 120
done
Why it works:
It executes your command, waits for 120 seconds (2 minutes), and then loops back to run it again.
The Gotcha:
If you close your SSH terminal window, the script dies.
Pro Tip: If you want to keep this simple loop running even after you log off, wrap it inside tmux or screen.
- Start a tmux session:
tmux - Run your
whileloop inside it. - Detach from the session by pressing
Ctrl + B, thenD.
Now you can safely disconnect from SSH, and the loop will keep running in the background.
Method 2: The Cron Job (Best for “Set It and Forget It”)
If you want a background task that runs automatically without keeping an active terminal session open, cron is the classic sysadmin solution.
How to do it:
- Open your crontab editor:
Bash
crontab -e
- Add this line at the bottom of the file:
Code snippet
*/2 * * * * rclone copy /workspace/runpod-slim/ComfyUI/output gdrive:SomeFolder/output/ > /tmp/rclone.log 2>&1
- Save and close the editor.
Why this setup matters:
*/2 * * * *: This tells cron to trigger every 2 minutes.> /tmp/rclone.log 2>&1: Notice we removed--progress. Cron doesn’t have an interactive screen to display progress bars, so we redirect the output to a log file instead. This keeps your system clean and lets you inspect errors later by runningcat /tmp/rclone.log.
The Common Mistake: overlapping jobs
What if your rclone sync takes 3 minutes because you uploaded a massive file, but cron tries to start another instance at minute 2?
To prevent two copy jobs from running at the exact same time and choking your CPU or bandwidth, lock the execution using flock:
Code snippet
*/2 * * * * flock -n /tmp/rclone.lock rclone copy /workspace/runpod-slim/ComfyUI/output gdrive:SomeFolder/output/ > /tmp/rclone.log 2>&1
If the previous sync is still running, flock simply skips the new execution until the next 2-minute cycle.
Method 3: Systemd Timer (The Rock-Solid Production Way)
Back when I was rebuilding my first 486 PC, hardware was unforgiving. Linux has come a long way since then, and today systemd is the modern standard for managing persistent background services.
If this command is critical to your workflow and you want it to automatically restart after a server reboot, write a simple systemd service and timer.
Step 1: Create the Service File
Create a file named /etc/systemd/system/rclone-sync.service:
Ini, TOML
[Unit]
Description=Rclone Sync Service
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/rclone copy /workspace/runpod-slim/ComfyUI/output gdrive:SomeFolder/output/
(Note: Always use full paths like /usr/bin/rclone inside systemd services.)
Step 2: Create the Timer File
Create another file named /etc/systemd/system/rclone-sync.timer:
Ini, TOML
[Unit]
Description=Run Rclone Sync every 2 minutes
[Timer]
OnBootSec=1min
OnUnitActiveSec=2min
Unit=rclone-sync.service
[Install]
WantedBy=timers.target
Step 3: Enable and Start the Timer
Reload systemd and start your timer:
Bash
sudo systemctl daemon-reload
sudo systemctl enable --now rclone-sync.timer
Now, systemd handles execution every 2 minutes, logs everything cleanly to journalctl, and handles restarts without missing a beat.
Which Method Should You Choose?
- Use the Shell Loop (
while true) inside atmuxsession if you just need a temporary test for a few hours. - Use Cron (
crontab -e) if you want a quick, set-and-forget task without messing with system configuration files. - Use Systemd Timers if this is for a production server where crash recovery and boot persistence are mandatory.
No matter which path you take, keep an eye on your file locks and disk usage. Happy syncing!
No Comments