Search code examples
loopsshcpu-usagedash-shell

How to limit the CPU usage of a dash loop that listens for a filesystem change?


I wrote a loop in dash which waits for a file to appear in a certain location on the filesystem. But when it runs it takes up 50% of the CPU. I first lowered the CPU usage using a sleep command but the minimal sleep time in dash (1 second) is too long for me.

I need a way to sleep less than 1 second in bash or a different solution to lower the CPU usage of the loop. I only want to use programs that are installed by default on the most unix systems.


Solution

  • On my Linux system, I can simply do

    $ sleep 0.1
    

    and it sleeps for 0.1 seconds. The sleep here lives in /usr/bin/sleep. I don't know which other unix-like systems support that, though.