Stop your scripts from chewing up CPU by looping poorly

Shell Scripting / Bash Tricks

Stop your scripts from chewing up CPU by looping poorly

🧩 The Challenge

You’ve probably written a script that polls a status or waits for a file to show up, and it ends up pinning a CPU core at 100% because the loop runs way too fast. I’ve definitely nuked a few production monitoring nodes this way before I learned my lesson.

💡 The Fix

Throw a small sleep interval into your loop to give the kernel a breather. It’s an easy way to keep your scripts from becoming a resource hog.

while [ ! -f /tmp/data.ready ]; do sleep 1; done

⚙️ Why It Works

Adding a delay allows the process to yield its execution slice back to the scheduler. Without that pause, the shell will just thrash the CPU in a tight spinlock until you kill it.

🚀 Pro-Tip: Use sleep 0.1 if you need it faster than a second but don’t want to kill your system performance.

Linux Tips & Tricks | © ngelinux.com | 10/16/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted