Shell: is it possible to delay a command without using `sleep`?











up vote
13
down vote

favorite
1












Are there any substitutes, alternatives or bash tricks for delaying commands without using sleep? For example, performing the below command without actually using sleep:



$ sleep 10 && echo "This is a test"









share|improve this question









New contributor




user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 28




    What's wrong with sleep?
    – muru
    yesterday






  • 2




    There's no real reason other than curiosity. I thought it would be interesting to learn some alternative solutions. I think at might be one, but I couldn't find any usage examples.
    – user321697
    yesterday










  • What do you want to wait for? If there’s an event you’re waiting for, you’d typically use a while loop, testing for the condition and sleeping for one second (or whatever makes sense). If you’re waiting for a child process to finish, then you can use the wait builtin. If it’s something else, do elaborate, please.
    – Grega Bremec
    23 hours ago










  • @user321697 “at” is to schedule single jobs. they are executed by the atd service, so they won’t pause your shell script. one use case for at would be to have it do something at a specified time (async) and create a marker file when it’s finished, while your script is waiting for that file to appear in a while loop. you could achieve a similar effect by scheduling a job to send your script a SIGCONT and then freezing your script by sending yourself a SIGSTOP.
    – Grega Bremec
    23 hours ago








  • 1




    I came here expecting everyone to suggest a spinlock. I'm pleasantly surprised by all the answers.
    – Daan van Hoek
    19 hours ago















up vote
13
down vote

favorite
1












Are there any substitutes, alternatives or bash tricks for delaying commands without using sleep? For example, performing the below command without actually using sleep:



$ sleep 10 && echo "This is a test"









share|improve this question









New contributor




user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 28




    What's wrong with sleep?
    – muru
    yesterday






  • 2




    There's no real reason other than curiosity. I thought it would be interesting to learn some alternative solutions. I think at might be one, but I couldn't find any usage examples.
    – user321697
    yesterday










  • What do you want to wait for? If there’s an event you’re waiting for, you’d typically use a while loop, testing for the condition and sleeping for one second (or whatever makes sense). If you’re waiting for a child process to finish, then you can use the wait builtin. If it’s something else, do elaborate, please.
    – Grega Bremec
    23 hours ago










  • @user321697 “at” is to schedule single jobs. they are executed by the atd service, so they won’t pause your shell script. one use case for at would be to have it do something at a specified time (async) and create a marker file when it’s finished, while your script is waiting for that file to appear in a while loop. you could achieve a similar effect by scheduling a job to send your script a SIGCONT and then freezing your script by sending yourself a SIGSTOP.
    – Grega Bremec
    23 hours ago








  • 1




    I came here expecting everyone to suggest a spinlock. I'm pleasantly surprised by all the answers.
    – Daan van Hoek
    19 hours ago













up vote
13
down vote

favorite
1









up vote
13
down vote

favorite
1






1





Are there any substitutes, alternatives or bash tricks for delaying commands without using sleep? For example, performing the below command without actually using sleep:



$ sleep 10 && echo "This is a test"









share|improve this question









New contributor




user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Are there any substitutes, alternatives or bash tricks for delaying commands without using sleep? For example, performing the below command without actually using sleep:



$ sleep 10 && echo "This is a test"






linux bash shell-script shell sleep






share|improve this question









New contributor




user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 23 hours ago









Fabby

2,94411125




2,94411125






New contributor




user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









user321697

764




764




New contributor




user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user321697 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 28




    What's wrong with sleep?
    – muru
    yesterday






  • 2




    There's no real reason other than curiosity. I thought it would be interesting to learn some alternative solutions. I think at might be one, but I couldn't find any usage examples.
    – user321697
    yesterday










  • What do you want to wait for? If there’s an event you’re waiting for, you’d typically use a while loop, testing for the condition and sleeping for one second (or whatever makes sense). If you’re waiting for a child process to finish, then you can use the wait builtin. If it’s something else, do elaborate, please.
    – Grega Bremec
    23 hours ago










  • @user321697 “at” is to schedule single jobs. they are executed by the atd service, so they won’t pause your shell script. one use case for at would be to have it do something at a specified time (async) and create a marker file when it’s finished, while your script is waiting for that file to appear in a while loop. you could achieve a similar effect by scheduling a job to send your script a SIGCONT and then freezing your script by sending yourself a SIGSTOP.
    – Grega Bremec
    23 hours ago








  • 1




    I came here expecting everyone to suggest a spinlock. I'm pleasantly surprised by all the answers.
    – Daan van Hoek
    19 hours ago














  • 28




    What's wrong with sleep?
    – muru
    yesterday






  • 2




    There's no real reason other than curiosity. I thought it would be interesting to learn some alternative solutions. I think at might be one, but I couldn't find any usage examples.
    – user321697
    yesterday










  • What do you want to wait for? If there’s an event you’re waiting for, you’d typically use a while loop, testing for the condition and sleeping for one second (or whatever makes sense). If you’re waiting for a child process to finish, then you can use the wait builtin. If it’s something else, do elaborate, please.
    – Grega Bremec
    23 hours ago










  • @user321697 “at” is to schedule single jobs. they are executed by the atd service, so they won’t pause your shell script. one use case for at would be to have it do something at a specified time (async) and create a marker file when it’s finished, while your script is waiting for that file to appear in a while loop. you could achieve a similar effect by scheduling a job to send your script a SIGCONT and then freezing your script by sending yourself a SIGSTOP.
    – Grega Bremec
    23 hours ago








  • 1




    I came here expecting everyone to suggest a spinlock. I'm pleasantly surprised by all the answers.
    – Daan van Hoek
    19 hours ago








28




28




What's wrong with sleep?
– muru
yesterday




What's wrong with sleep?
– muru
yesterday




2




2




There's no real reason other than curiosity. I thought it would be interesting to learn some alternative solutions. I think at might be one, but I couldn't find any usage examples.
– user321697
yesterday




There's no real reason other than curiosity. I thought it would be interesting to learn some alternative solutions. I think at might be one, but I couldn't find any usage examples.
– user321697
yesterday












What do you want to wait for? If there’s an event you’re waiting for, you’d typically use a while loop, testing for the condition and sleeping for one second (or whatever makes sense). If you’re waiting for a child process to finish, then you can use the wait builtin. If it’s something else, do elaborate, please.
– Grega Bremec
23 hours ago




What do you want to wait for? If there’s an event you’re waiting for, you’d typically use a while loop, testing for the condition and sleeping for one second (or whatever makes sense). If you’re waiting for a child process to finish, then you can use the wait builtin. If it’s something else, do elaborate, please.
– Grega Bremec
23 hours ago












@user321697 “at” is to schedule single jobs. they are executed by the atd service, so they won’t pause your shell script. one use case for at would be to have it do something at a specified time (async) and create a marker file when it’s finished, while your script is waiting for that file to appear in a while loop. you could achieve a similar effect by scheduling a job to send your script a SIGCONT and then freezing your script by sending yourself a SIGSTOP.
– Grega Bremec
23 hours ago






@user321697 “at” is to schedule single jobs. they are executed by the atd service, so they won’t pause your shell script. one use case for at would be to have it do something at a specified time (async) and create a marker file when it’s finished, while your script is waiting for that file to appear in a while loop. you could achieve a similar effect by scheduling a job to send your script a SIGCONT and then freezing your script by sending yourself a SIGSTOP.
– Grega Bremec
23 hours ago






1




1




I came here expecting everyone to suggest a spinlock. I'm pleasantly surprised by all the answers.
– Daan van Hoek
19 hours ago




I came here expecting everyone to suggest a spinlock. I'm pleasantly surprised by all the answers.
– Daan van Hoek
19 hours ago










9 Answers
9






active

oldest

votes

















up vote
8
down vote



accepted










You have alternatives to sleep: They are at and cron. Contrary to sleep these need you to provide the time at which you need them to run.




  • Make sure the atd service is running by executing service atd status.

    Now let's say the date is 11:17 am UTC; if you need to execute a command at 11:25 UTC, the syntax is: echo "This is a test" | at 11:25.

    Now keep in mind that atd by default will not be logging the completion of the jobs. For more refer this link. It's better that your application has its own logging.


  • You can schedule jobs in cron, for more refer : man cron to see its options or crontab -e to add new jobs. /var/log/cron can be checked for the info on execution on jobs.



FYI sleep system call suspends the current execution and schedules it w.r.t. the argument passed to it.



EDIT:



As @Gaius mentioned , you can also add minutes time to at command.But lets say time is 12:30:30 and now you ran the scheduler with now +1 minutes. Even though 1 minute, which translates to 60 seconds was specified , the at doesn't really wait till 12:31:30 to execute the job, rather it executes the job at 12:31:00. The time-units can be minutes, hours, days, or weeks. For more refer man at



e.g: echo "ls" | at now +1 minutes






share|improve this answer



















  • 4




    This is not true, you can schedule an at job for say now +1 minute, to run in a minutes time
    – Gaius
    18 hours ago


















up vote
16
down vote













With bash builtins, you can do:



coproc read -t 10 && wait "$!" || true


To sleep for 10 seconds without using sleep. The coproc is to make so that read's stdin is a pipe where nothing will ever come out from. || true is because wait's exit status will reflect a SIGALRM delivery which would cause the shell to exit if the errexit option is set.



In other shells:



mksh and ksh93 have sleep built-in, no point in using anything else there (though they both also support read -t).



zsh also supports read -t, but also has a builtin wrapper around select(), so you can also use:



zmodload zsh/zselect
zselect -t 1000 # centiseconds


If what you want is schedule things to be run from an interactive shell session, see also the zsh/sched module in zsh.






share|improve this answer























  • Would you consider read -t 10 < /dev/zero || true ?
    – Jeff Schaller
    20 hours ago








  • 5




    @JeffSchaller I would avoid it as that's a busy loop.
    – Stéphane Chazelas
    20 hours ago










  • @StéphaneChazelas I wouldn't expect that to be a busy loop – I'd expect any shell's read to be implemented using select(2) or something similar (implying that read-with-timeout would be a good answer to this question). I'm expressing surprise rather than contradicting you, but can you point to further discussion of this?
    – Norman Gray
    19 hours ago






  • 2




    @NormanGray, /dev/zero is a file that contains an infinite amount of data (NUL bytes). So read will read as much as it can during those 10 seconds. Thankfully, in the case of bash which doesn't support storing NUL bytes in its variables, that won't use up any memory, but that will still hog CPU resources.
    – Stéphane Chazelas
    19 hours ago






  • 1




    @NormanGray, if run from a terminal, /dev/stdout would be the tty device, so it would have side effects (like stopping the script if run in background) and would return if the user presses enter for instance. read -t 10 /dev/stdout | : would work on Linux, but on Linux only, while coproc should work regardless of the OS.
    – Stéphane Chazelas
    19 hours ago




















up vote
5
down vote













Using the bash built-in variable $SECONDS and a busy-loop:



for((target=$((SECONDS + 10)); SECONDS < target; true)); do :; done





share|improve this answer

















  • 2




    That would in effect pause for a duration ranging somewhere in between 9 and 10 seconds though (due to a bug in bash; zsh and mksh had similar issues but have been fixed since)
    – Stéphane Chazelas
    21 hours ago








  • 4




    A good way to make heat.
    – ctrl-alt-delor
    20 hours ago






  • 4




    won't be the first time I'm accused of being full of hot air! :)
    – Jeff Schaller
    20 hours ago


















up vote
5
down vote













Since there are answers which are suggesting to use the non-standard -t delay option of read, here is a way to do a timed-out read in a standard shell:



{ ss=`stty -g`; stty -icanon min 0 time 20; read foo; stty "$ss"; }


The argument to stty time is in tenths of second.






share|improve this answer




























    up vote
    3
    down vote













    There is no built-in, that does the same as sleep (unless sleep is built-in). However there are some other commands that will wait.



    A few include.




    • at and cron: used to schedule tasks at a specific time.


    • inotifywait: used to wait for a file, or files to be modified/removed/added/etc







    share|improve this answer























    • Thanks for this. Could you provide an example for performing a scheduled task (10 seconds from now) with at?
      – user321697
      23 hours ago






    • 1




      an edit and an upvote! ;-)
      – Fabby
      23 hours ago










    • cron store tasks in crontab, right? Where does at store the scheduled data?
      – user321697
      23 hours ago










    • @user321697 Already answered here
      – Fabby
      23 hours ago










    • Sorry for reverting back your edit to the Q: you changed the OPs question's main purpose which would invalidate the simplest answer of all... ¯_(ツ)_/¯
      – Fabby
      23 hours ago




















    up vote
    2
    down vote













    the oldest trick in the book:



    read && echo "This is a test"


    Just hit Enter and it'll continue!






    share|improve this answer





















    • This wont if the process needs to be run interaction free or in the background right?
      – sai sasanka
      23 hours ago










    • @saisasanka Deleted my superfluous comments please ping me in chat after you've done the same, so I can remove this last one... ;-)
      – Fabby
      22 hours ago




















    up vote
    2
    down vote













    Back in the days of microcomputers running BASIC, delays were usually accomplished with an empty loop:



    FOR I = 1 TO 10000:NEXT



    The same principle could be used to insert a delay in a shell script:



    COUNTER=0; while [ $COUNTER -lt 10000 ]; do :; let COUNTER=COUNTER+1; done



    Of course, the problem with this approach is that the length of the delay will vary from machine to machine according to its processor speed (or even on the same machine under different loads). Unlike sleep, it will probably also max out your CPU (or one of its cores).






    share|improve this answer

















    • 2




      A good way to make heat.
      – ctrl-alt-delor
      20 hours ago










    • Delay-loops are a terrible idea for anything except the very shortest of sleeps (a couple nanoseconds or clock cycles in a device driver) on any modern CPU that can run a Unix-like OS. i.e. a sleep so short you can't usefully have the CPU do anything else while waiting, like schedule another process or enter a low-power sleep state before waking on a timer interrupt. Dynamic CPU-frequency makes it impossible to even calibrate a delay loop for counts per second, except as a minimum delay potentially sleeping a lot longer at low clock speeds before ramping up.
      – Peter Cordes
      12 hours ago










    • Ancient computers had a power consumption that was much less dependent on workload. Modern CPUs need to dynamically power down different parts of the chip as much as possible to not melt (e.g. power down parts of the FPU or SIMD execution units while only integer code is running, or at least gate the clock signal to parts of the chip that don't need to be switching). And entering a low-power sleep state when idle (instead of spinning in an infinite loop waiting for timer interrupts) is also more recent than the ancient computers you mention.
      – Peter Cordes
      12 hours ago










    • For more about CPU history, see Modern Microprocessors A 90-Minute Guide! - lighterra.com/papers/modernmicroprocessors.
      – Peter Cordes
      12 hours ago


















    up vote
    1
    down vote













    Some other ideas.



    top -d10 -n2 >/dev/null

    vmstat 10 2 >/dev/null

    sar 10 1 >/dev/null

    timeout 10s tail -f /dev/null





    share|improve this answer




























      up vote
      0
      down vote













      A classic from the Land of Windows and Batches:



      ping -c 11 localhost >/dev/null && echo "This is a test"





      share|improve this answer








      New contributor




      Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "106"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });






        user321697 is a new contributor. Be nice, and check out our Code of Conduct.










         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482725%2fshell-is-it-possible-to-delay-a-command-without-using-sleep%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        9 Answers
        9






        active

        oldest

        votes








        9 Answers
        9






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        8
        down vote



        accepted










        You have alternatives to sleep: They are at and cron. Contrary to sleep these need you to provide the time at which you need them to run.




        • Make sure the atd service is running by executing service atd status.

          Now let's say the date is 11:17 am UTC; if you need to execute a command at 11:25 UTC, the syntax is: echo "This is a test" | at 11:25.

          Now keep in mind that atd by default will not be logging the completion of the jobs. For more refer this link. It's better that your application has its own logging.


        • You can schedule jobs in cron, for more refer : man cron to see its options or crontab -e to add new jobs. /var/log/cron can be checked for the info on execution on jobs.



        FYI sleep system call suspends the current execution and schedules it w.r.t. the argument passed to it.



        EDIT:



        As @Gaius mentioned , you can also add minutes time to at command.But lets say time is 12:30:30 and now you ran the scheduler with now +1 minutes. Even though 1 minute, which translates to 60 seconds was specified , the at doesn't really wait till 12:31:30 to execute the job, rather it executes the job at 12:31:00. The time-units can be minutes, hours, days, or weeks. For more refer man at



        e.g: echo "ls" | at now +1 minutes






        share|improve this answer



















        • 4




          This is not true, you can schedule an at job for say now +1 minute, to run in a minutes time
          – Gaius
          18 hours ago















        up vote
        8
        down vote



        accepted










        You have alternatives to sleep: They are at and cron. Contrary to sleep these need you to provide the time at which you need them to run.




        • Make sure the atd service is running by executing service atd status.

          Now let's say the date is 11:17 am UTC; if you need to execute a command at 11:25 UTC, the syntax is: echo "This is a test" | at 11:25.

          Now keep in mind that atd by default will not be logging the completion of the jobs. For more refer this link. It's better that your application has its own logging.


        • You can schedule jobs in cron, for more refer : man cron to see its options or crontab -e to add new jobs. /var/log/cron can be checked for the info on execution on jobs.



        FYI sleep system call suspends the current execution and schedules it w.r.t. the argument passed to it.



        EDIT:



        As @Gaius mentioned , you can also add minutes time to at command.But lets say time is 12:30:30 and now you ran the scheduler with now +1 minutes. Even though 1 minute, which translates to 60 seconds was specified , the at doesn't really wait till 12:31:30 to execute the job, rather it executes the job at 12:31:00. The time-units can be minutes, hours, days, or weeks. For more refer man at



        e.g: echo "ls" | at now +1 minutes






        share|improve this answer



















        • 4




          This is not true, you can schedule an at job for say now +1 minute, to run in a minutes time
          – Gaius
          18 hours ago













        up vote
        8
        down vote



        accepted







        up vote
        8
        down vote



        accepted






        You have alternatives to sleep: They are at and cron. Contrary to sleep these need you to provide the time at which you need them to run.




        • Make sure the atd service is running by executing service atd status.

          Now let's say the date is 11:17 am UTC; if you need to execute a command at 11:25 UTC, the syntax is: echo "This is a test" | at 11:25.

          Now keep in mind that atd by default will not be logging the completion of the jobs. For more refer this link. It's better that your application has its own logging.


        • You can schedule jobs in cron, for more refer : man cron to see its options or crontab -e to add new jobs. /var/log/cron can be checked for the info on execution on jobs.



        FYI sleep system call suspends the current execution and schedules it w.r.t. the argument passed to it.



        EDIT:



        As @Gaius mentioned , you can also add minutes time to at command.But lets say time is 12:30:30 and now you ran the scheduler with now +1 minutes. Even though 1 minute, which translates to 60 seconds was specified , the at doesn't really wait till 12:31:30 to execute the job, rather it executes the job at 12:31:00. The time-units can be minutes, hours, days, or weeks. For more refer man at



        e.g: echo "ls" | at now +1 minutes






        share|improve this answer














        You have alternatives to sleep: They are at and cron. Contrary to sleep these need you to provide the time at which you need them to run.




        • Make sure the atd service is running by executing service atd status.

          Now let's say the date is 11:17 am UTC; if you need to execute a command at 11:25 UTC, the syntax is: echo "This is a test" | at 11:25.

          Now keep in mind that atd by default will not be logging the completion of the jobs. For more refer this link. It's better that your application has its own logging.


        • You can schedule jobs in cron, for more refer : man cron to see its options or crontab -e to add new jobs. /var/log/cron can be checked for the info on execution on jobs.



        FYI sleep system call suspends the current execution and schedules it w.r.t. the argument passed to it.



        EDIT:



        As @Gaius mentioned , you can also add minutes time to at command.But lets say time is 12:30:30 and now you ran the scheduler with now +1 minutes. Even though 1 minute, which translates to 60 seconds was specified , the at doesn't really wait till 12:31:30 to execute the job, rather it executes the job at 12:31:00. The time-units can be minutes, hours, days, or weeks. For more refer man at



        e.g: echo "ls" | at now +1 minutes







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 17 hours ago

























        answered 23 hours ago









        sai sasanka

        64919




        64919








        • 4




          This is not true, you can schedule an at job for say now +1 minute, to run in a minutes time
          – Gaius
          18 hours ago














        • 4




          This is not true, you can schedule an at job for say now +1 minute, to run in a minutes time
          – Gaius
          18 hours ago








        4




        4




        This is not true, you can schedule an at job for say now +1 minute, to run in a minutes time
        – Gaius
        18 hours ago




        This is not true, you can schedule an at job for say now +1 minute, to run in a minutes time
        – Gaius
        18 hours ago












        up vote
        16
        down vote













        With bash builtins, you can do:



        coproc read -t 10 && wait "$!" || true


        To sleep for 10 seconds without using sleep. The coproc is to make so that read's stdin is a pipe where nothing will ever come out from. || true is because wait's exit status will reflect a SIGALRM delivery which would cause the shell to exit if the errexit option is set.



        In other shells:



        mksh and ksh93 have sleep built-in, no point in using anything else there (though they both also support read -t).



        zsh also supports read -t, but also has a builtin wrapper around select(), so you can also use:



        zmodload zsh/zselect
        zselect -t 1000 # centiseconds


        If what you want is schedule things to be run from an interactive shell session, see also the zsh/sched module in zsh.






        share|improve this answer























        • Would you consider read -t 10 < /dev/zero || true ?
          – Jeff Schaller
          20 hours ago








        • 5




          @JeffSchaller I would avoid it as that's a busy loop.
          – Stéphane Chazelas
          20 hours ago










        • @StéphaneChazelas I wouldn't expect that to be a busy loop – I'd expect any shell's read to be implemented using select(2) or something similar (implying that read-with-timeout would be a good answer to this question). I'm expressing surprise rather than contradicting you, but can you point to further discussion of this?
          – Norman Gray
          19 hours ago






        • 2




          @NormanGray, /dev/zero is a file that contains an infinite amount of data (NUL bytes). So read will read as much as it can during those 10 seconds. Thankfully, in the case of bash which doesn't support storing NUL bytes in its variables, that won't use up any memory, but that will still hog CPU resources.
          – Stéphane Chazelas
          19 hours ago






        • 1




          @NormanGray, if run from a terminal, /dev/stdout would be the tty device, so it would have side effects (like stopping the script if run in background) and would return if the user presses enter for instance. read -t 10 /dev/stdout | : would work on Linux, but on Linux only, while coproc should work regardless of the OS.
          – Stéphane Chazelas
          19 hours ago

















        up vote
        16
        down vote













        With bash builtins, you can do:



        coproc read -t 10 && wait "$!" || true


        To sleep for 10 seconds without using sleep. The coproc is to make so that read's stdin is a pipe where nothing will ever come out from. || true is because wait's exit status will reflect a SIGALRM delivery which would cause the shell to exit if the errexit option is set.



        In other shells:



        mksh and ksh93 have sleep built-in, no point in using anything else there (though they both also support read -t).



        zsh also supports read -t, but also has a builtin wrapper around select(), so you can also use:



        zmodload zsh/zselect
        zselect -t 1000 # centiseconds


        If what you want is schedule things to be run from an interactive shell session, see also the zsh/sched module in zsh.






        share|improve this answer























        • Would you consider read -t 10 < /dev/zero || true ?
          – Jeff Schaller
          20 hours ago








        • 5




          @JeffSchaller I would avoid it as that's a busy loop.
          – Stéphane Chazelas
          20 hours ago










        • @StéphaneChazelas I wouldn't expect that to be a busy loop – I'd expect any shell's read to be implemented using select(2) or something similar (implying that read-with-timeout would be a good answer to this question). I'm expressing surprise rather than contradicting you, but can you point to further discussion of this?
          – Norman Gray
          19 hours ago






        • 2




          @NormanGray, /dev/zero is a file that contains an infinite amount of data (NUL bytes). So read will read as much as it can during those 10 seconds. Thankfully, in the case of bash which doesn't support storing NUL bytes in its variables, that won't use up any memory, but that will still hog CPU resources.
          – Stéphane Chazelas
          19 hours ago






        • 1




          @NormanGray, if run from a terminal, /dev/stdout would be the tty device, so it would have side effects (like stopping the script if run in background) and would return if the user presses enter for instance. read -t 10 /dev/stdout | : would work on Linux, but on Linux only, while coproc should work regardless of the OS.
          – Stéphane Chazelas
          19 hours ago















        up vote
        16
        down vote










        up vote
        16
        down vote









        With bash builtins, you can do:



        coproc read -t 10 && wait "$!" || true


        To sleep for 10 seconds without using sleep. The coproc is to make so that read's stdin is a pipe where nothing will ever come out from. || true is because wait's exit status will reflect a SIGALRM delivery which would cause the shell to exit if the errexit option is set.



        In other shells:



        mksh and ksh93 have sleep built-in, no point in using anything else there (though they both also support read -t).



        zsh also supports read -t, but also has a builtin wrapper around select(), so you can also use:



        zmodload zsh/zselect
        zselect -t 1000 # centiseconds


        If what you want is schedule things to be run from an interactive shell session, see also the zsh/sched module in zsh.






        share|improve this answer














        With bash builtins, you can do:



        coproc read -t 10 && wait "$!" || true


        To sleep for 10 seconds without using sleep. The coproc is to make so that read's stdin is a pipe where nothing will ever come out from. || true is because wait's exit status will reflect a SIGALRM delivery which would cause the shell to exit if the errexit option is set.



        In other shells:



        mksh and ksh93 have sleep built-in, no point in using anything else there (though they both also support read -t).



        zsh also supports read -t, but also has a builtin wrapper around select(), so you can also use:



        zmodload zsh/zselect
        zselect -t 1000 # centiseconds


        If what you want is schedule things to be run from an interactive shell session, see also the zsh/sched module in zsh.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 22 hours ago

























        answered 23 hours ago









        Stéphane Chazelas

        294k54551893




        294k54551893












        • Would you consider read -t 10 < /dev/zero || true ?
          – Jeff Schaller
          20 hours ago








        • 5




          @JeffSchaller I would avoid it as that's a busy loop.
          – Stéphane Chazelas
          20 hours ago










        • @StéphaneChazelas I wouldn't expect that to be a busy loop – I'd expect any shell's read to be implemented using select(2) or something similar (implying that read-with-timeout would be a good answer to this question). I'm expressing surprise rather than contradicting you, but can you point to further discussion of this?
          – Norman Gray
          19 hours ago






        • 2




          @NormanGray, /dev/zero is a file that contains an infinite amount of data (NUL bytes). So read will read as much as it can during those 10 seconds. Thankfully, in the case of bash which doesn't support storing NUL bytes in its variables, that won't use up any memory, but that will still hog CPU resources.
          – Stéphane Chazelas
          19 hours ago






        • 1




          @NormanGray, if run from a terminal, /dev/stdout would be the tty device, so it would have side effects (like stopping the script if run in background) and would return if the user presses enter for instance. read -t 10 /dev/stdout | : would work on Linux, but on Linux only, while coproc should work regardless of the OS.
          – Stéphane Chazelas
          19 hours ago




















        • Would you consider read -t 10 < /dev/zero || true ?
          – Jeff Schaller
          20 hours ago








        • 5




          @JeffSchaller I would avoid it as that's a busy loop.
          – Stéphane Chazelas
          20 hours ago










        • @StéphaneChazelas I wouldn't expect that to be a busy loop – I'd expect any shell's read to be implemented using select(2) or something similar (implying that read-with-timeout would be a good answer to this question). I'm expressing surprise rather than contradicting you, but can you point to further discussion of this?
          – Norman Gray
          19 hours ago






        • 2




          @NormanGray, /dev/zero is a file that contains an infinite amount of data (NUL bytes). So read will read as much as it can during those 10 seconds. Thankfully, in the case of bash which doesn't support storing NUL bytes in its variables, that won't use up any memory, but that will still hog CPU resources.
          – Stéphane Chazelas
          19 hours ago






        • 1




          @NormanGray, if run from a terminal, /dev/stdout would be the tty device, so it would have side effects (like stopping the script if run in background) and would return if the user presses enter for instance. read -t 10 /dev/stdout | : would work on Linux, but on Linux only, while coproc should work regardless of the OS.
          – Stéphane Chazelas
          19 hours ago


















        Would you consider read -t 10 < /dev/zero || true ?
        – Jeff Schaller
        20 hours ago






        Would you consider read -t 10 < /dev/zero || true ?
        – Jeff Schaller
        20 hours ago






        5




        5




        @JeffSchaller I would avoid it as that's a busy loop.
        – Stéphane Chazelas
        20 hours ago




        @JeffSchaller I would avoid it as that's a busy loop.
        – Stéphane Chazelas
        20 hours ago












        @StéphaneChazelas I wouldn't expect that to be a busy loop – I'd expect any shell's read to be implemented using select(2) or something similar (implying that read-with-timeout would be a good answer to this question). I'm expressing surprise rather than contradicting you, but can you point to further discussion of this?
        – Norman Gray
        19 hours ago




        @StéphaneChazelas I wouldn't expect that to be a busy loop – I'd expect any shell's read to be implemented using select(2) or something similar (implying that read-with-timeout would be a good answer to this question). I'm expressing surprise rather than contradicting you, but can you point to further discussion of this?
        – Norman Gray
        19 hours ago




        2




        2




        @NormanGray, /dev/zero is a file that contains an infinite amount of data (NUL bytes). So read will read as much as it can during those 10 seconds. Thankfully, in the case of bash which doesn't support storing NUL bytes in its variables, that won't use up any memory, but that will still hog CPU resources.
        – Stéphane Chazelas
        19 hours ago




        @NormanGray, /dev/zero is a file that contains an infinite amount of data (NUL bytes). So read will read as much as it can during those 10 seconds. Thankfully, in the case of bash which doesn't support storing NUL bytes in its variables, that won't use up any memory, but that will still hog CPU resources.
        – Stéphane Chazelas
        19 hours ago




        1




        1




        @NormanGray, if run from a terminal, /dev/stdout would be the tty device, so it would have side effects (like stopping the script if run in background) and would return if the user presses enter for instance. read -t 10 /dev/stdout | : would work on Linux, but on Linux only, while coproc should work regardless of the OS.
        – Stéphane Chazelas
        19 hours ago






        @NormanGray, if run from a terminal, /dev/stdout would be the tty device, so it would have side effects (like stopping the script if run in background) and would return if the user presses enter for instance. read -t 10 /dev/stdout | : would work on Linux, but on Linux only, while coproc should work regardless of the OS.
        – Stéphane Chazelas
        19 hours ago












        up vote
        5
        down vote













        Using the bash built-in variable $SECONDS and a busy-loop:



        for((target=$((SECONDS + 10)); SECONDS < target; true)); do :; done





        share|improve this answer

















        • 2




          That would in effect pause for a duration ranging somewhere in between 9 and 10 seconds though (due to a bug in bash; zsh and mksh had similar issues but have been fixed since)
          – Stéphane Chazelas
          21 hours ago








        • 4




          A good way to make heat.
          – ctrl-alt-delor
          20 hours ago






        • 4




          won't be the first time I'm accused of being full of hot air! :)
          – Jeff Schaller
          20 hours ago















        up vote
        5
        down vote













        Using the bash built-in variable $SECONDS and a busy-loop:



        for((target=$((SECONDS + 10)); SECONDS < target; true)); do :; done





        share|improve this answer

















        • 2




          That would in effect pause for a duration ranging somewhere in between 9 and 10 seconds though (due to a bug in bash; zsh and mksh had similar issues but have been fixed since)
          – Stéphane Chazelas
          21 hours ago








        • 4




          A good way to make heat.
          – ctrl-alt-delor
          20 hours ago






        • 4




          won't be the first time I'm accused of being full of hot air! :)
          – Jeff Schaller
          20 hours ago













        up vote
        5
        down vote










        up vote
        5
        down vote









        Using the bash built-in variable $SECONDS and a busy-loop:



        for((target=$((SECONDS + 10)); SECONDS < target; true)); do :; done





        share|improve this answer












        Using the bash built-in variable $SECONDS and a busy-loop:



        for((target=$((SECONDS + 10)); SECONDS < target; true)); do :; done






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 21 hours ago









        Jeff Schaller

        36.1k952119




        36.1k952119








        • 2




          That would in effect pause for a duration ranging somewhere in between 9 and 10 seconds though (due to a bug in bash; zsh and mksh had similar issues but have been fixed since)
          – Stéphane Chazelas
          21 hours ago








        • 4




          A good way to make heat.
          – ctrl-alt-delor
          20 hours ago






        • 4




          won't be the first time I'm accused of being full of hot air! :)
          – Jeff Schaller
          20 hours ago














        • 2




          That would in effect pause for a duration ranging somewhere in between 9 and 10 seconds though (due to a bug in bash; zsh and mksh had similar issues but have been fixed since)
          – Stéphane Chazelas
          21 hours ago








        • 4




          A good way to make heat.
          – ctrl-alt-delor
          20 hours ago






        • 4




          won't be the first time I'm accused of being full of hot air! :)
          – Jeff Schaller
          20 hours ago








        2




        2




        That would in effect pause for a duration ranging somewhere in between 9 and 10 seconds though (due to a bug in bash; zsh and mksh had similar issues but have been fixed since)
        – Stéphane Chazelas
        21 hours ago






        That would in effect pause for a duration ranging somewhere in between 9 and 10 seconds though (due to a bug in bash; zsh and mksh had similar issues but have been fixed since)
        – Stéphane Chazelas
        21 hours ago






        4




        4




        A good way to make heat.
        – ctrl-alt-delor
        20 hours ago




        A good way to make heat.
        – ctrl-alt-delor
        20 hours ago




        4




        4




        won't be the first time I'm accused of being full of hot air! :)
        – Jeff Schaller
        20 hours ago




        won't be the first time I'm accused of being full of hot air! :)
        – Jeff Schaller
        20 hours ago










        up vote
        5
        down vote













        Since there are answers which are suggesting to use the non-standard -t delay option of read, here is a way to do a timed-out read in a standard shell:



        { ss=`stty -g`; stty -icanon min 0 time 20; read foo; stty "$ss"; }


        The argument to stty time is in tenths of second.






        share|improve this answer

























          up vote
          5
          down vote













          Since there are answers which are suggesting to use the non-standard -t delay option of read, here is a way to do a timed-out read in a standard shell:



          { ss=`stty -g`; stty -icanon min 0 time 20; read foo; stty "$ss"; }


          The argument to stty time is in tenths of second.






          share|improve this answer























            up vote
            5
            down vote










            up vote
            5
            down vote









            Since there are answers which are suggesting to use the non-standard -t delay option of read, here is a way to do a timed-out read in a standard shell:



            { ss=`stty -g`; stty -icanon min 0 time 20; read foo; stty "$ss"; }


            The argument to stty time is in tenths of second.






            share|improve this answer












            Since there are answers which are suggesting to use the non-standard -t delay option of read, here is a way to do a timed-out read in a standard shell:



            { ss=`stty -g`; stty -icanon min 0 time 20; read foo; stty "$ss"; }


            The argument to stty time is in tenths of second.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 17 hours ago









            mosvy

            4,323221




            4,323221






















                up vote
                3
                down vote













                There is no built-in, that does the same as sleep (unless sleep is built-in). However there are some other commands that will wait.



                A few include.




                • at and cron: used to schedule tasks at a specific time.


                • inotifywait: used to wait for a file, or files to be modified/removed/added/etc







                share|improve this answer























                • Thanks for this. Could you provide an example for performing a scheduled task (10 seconds from now) with at?
                  – user321697
                  23 hours ago






                • 1




                  an edit and an upvote! ;-)
                  – Fabby
                  23 hours ago










                • cron store tasks in crontab, right? Where does at store the scheduled data?
                  – user321697
                  23 hours ago










                • @user321697 Already answered here
                  – Fabby
                  23 hours ago










                • Sorry for reverting back your edit to the Q: you changed the OPs question's main purpose which would invalidate the simplest answer of all... ¯_(ツ)_/¯
                  – Fabby
                  23 hours ago

















                up vote
                3
                down vote













                There is no built-in, that does the same as sleep (unless sleep is built-in). However there are some other commands that will wait.



                A few include.




                • at and cron: used to schedule tasks at a specific time.


                • inotifywait: used to wait for a file, or files to be modified/removed/added/etc







                share|improve this answer























                • Thanks for this. Could you provide an example for performing a scheduled task (10 seconds from now) with at?
                  – user321697
                  23 hours ago






                • 1




                  an edit and an upvote! ;-)
                  – Fabby
                  23 hours ago










                • cron store tasks in crontab, right? Where does at store the scheduled data?
                  – user321697
                  23 hours ago










                • @user321697 Already answered here
                  – Fabby
                  23 hours ago










                • Sorry for reverting back your edit to the Q: you changed the OPs question's main purpose which would invalidate the simplest answer of all... ¯_(ツ)_/¯
                  – Fabby
                  23 hours ago















                up vote
                3
                down vote










                up vote
                3
                down vote









                There is no built-in, that does the same as sleep (unless sleep is built-in). However there are some other commands that will wait.



                A few include.




                • at and cron: used to schedule tasks at a specific time.


                • inotifywait: used to wait for a file, or files to be modified/removed/added/etc







                share|improve this answer














                There is no built-in, that does the same as sleep (unless sleep is built-in). However there are some other commands that will wait.



                A few include.




                • at and cron: used to schedule tasks at a specific time.


                • inotifywait: used to wait for a file, or files to be modified/removed/added/etc








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 23 hours ago









                Fabby

                2,94411125




                2,94411125










                answered 23 hours ago









                ctrl-alt-delor

                9,96331954




                9,96331954












                • Thanks for this. Could you provide an example for performing a scheduled task (10 seconds from now) with at?
                  – user321697
                  23 hours ago






                • 1




                  an edit and an upvote! ;-)
                  – Fabby
                  23 hours ago










                • cron store tasks in crontab, right? Where does at store the scheduled data?
                  – user321697
                  23 hours ago










                • @user321697 Already answered here
                  – Fabby
                  23 hours ago










                • Sorry for reverting back your edit to the Q: you changed the OPs question's main purpose which would invalidate the simplest answer of all... ¯_(ツ)_/¯
                  – Fabby
                  23 hours ago




















                • Thanks for this. Could you provide an example for performing a scheduled task (10 seconds from now) with at?
                  – user321697
                  23 hours ago






                • 1




                  an edit and an upvote! ;-)
                  – Fabby
                  23 hours ago










                • cron store tasks in crontab, right? Where does at store the scheduled data?
                  – user321697
                  23 hours ago










                • @user321697 Already answered here
                  – Fabby
                  23 hours ago










                • Sorry for reverting back your edit to the Q: you changed the OPs question's main purpose which would invalidate the simplest answer of all... ¯_(ツ)_/¯
                  – Fabby
                  23 hours ago


















                Thanks for this. Could you provide an example for performing a scheduled task (10 seconds from now) with at?
                – user321697
                23 hours ago




                Thanks for this. Could you provide an example for performing a scheduled task (10 seconds from now) with at?
                – user321697
                23 hours ago




                1




                1




                an edit and an upvote! ;-)
                – Fabby
                23 hours ago




                an edit and an upvote! ;-)
                – Fabby
                23 hours ago












                cron store tasks in crontab, right? Where does at store the scheduled data?
                – user321697
                23 hours ago




                cron store tasks in crontab, right? Where does at store the scheduled data?
                – user321697
                23 hours ago












                @user321697 Already answered here
                – Fabby
                23 hours ago




                @user321697 Already answered here
                – Fabby
                23 hours ago












                Sorry for reverting back your edit to the Q: you changed the OPs question's main purpose which would invalidate the simplest answer of all... ¯_(ツ)_/¯
                – Fabby
                23 hours ago






                Sorry for reverting back your edit to the Q: you changed the OPs question's main purpose which would invalidate the simplest answer of all... ¯_(ツ)_/¯
                – Fabby
                23 hours ago












                up vote
                2
                down vote













                the oldest trick in the book:



                read && echo "This is a test"


                Just hit Enter and it'll continue!






                share|improve this answer





















                • This wont if the process needs to be run interaction free or in the background right?
                  – sai sasanka
                  23 hours ago










                • @saisasanka Deleted my superfluous comments please ping me in chat after you've done the same, so I can remove this last one... ;-)
                  – Fabby
                  22 hours ago

















                up vote
                2
                down vote













                the oldest trick in the book:



                read && echo "This is a test"


                Just hit Enter and it'll continue!






                share|improve this answer





















                • This wont if the process needs to be run interaction free or in the background right?
                  – sai sasanka
                  23 hours ago










                • @saisasanka Deleted my superfluous comments please ping me in chat after you've done the same, so I can remove this last one... ;-)
                  – Fabby
                  22 hours ago















                up vote
                2
                down vote










                up vote
                2
                down vote









                the oldest trick in the book:



                read && echo "This is a test"


                Just hit Enter and it'll continue!






                share|improve this answer












                the oldest trick in the book:



                read && echo "This is a test"


                Just hit Enter and it'll continue!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 23 hours ago









                Fabby

                2,94411125




                2,94411125












                • This wont if the process needs to be run interaction free or in the background right?
                  – sai sasanka
                  23 hours ago










                • @saisasanka Deleted my superfluous comments please ping me in chat after you've done the same, so I can remove this last one... ;-)
                  – Fabby
                  22 hours ago




















                • This wont if the process needs to be run interaction free or in the background right?
                  – sai sasanka
                  23 hours ago










                • @saisasanka Deleted my superfluous comments please ping me in chat after you've done the same, so I can remove this last one... ;-)
                  – Fabby
                  22 hours ago


















                This wont if the process needs to be run interaction free or in the background right?
                – sai sasanka
                23 hours ago




                This wont if the process needs to be run interaction free or in the background right?
                – sai sasanka
                23 hours ago












                @saisasanka Deleted my superfluous comments please ping me in chat after you've done the same, so I can remove this last one... ;-)
                – Fabby
                22 hours ago






                @saisasanka Deleted my superfluous comments please ping me in chat after you've done the same, so I can remove this last one... ;-)
                – Fabby
                22 hours ago












                up vote
                2
                down vote













                Back in the days of microcomputers running BASIC, delays were usually accomplished with an empty loop:



                FOR I = 1 TO 10000:NEXT



                The same principle could be used to insert a delay in a shell script:



                COUNTER=0; while [ $COUNTER -lt 10000 ]; do :; let COUNTER=COUNTER+1; done



                Of course, the problem with this approach is that the length of the delay will vary from machine to machine according to its processor speed (or even on the same machine under different loads). Unlike sleep, it will probably also max out your CPU (or one of its cores).






                share|improve this answer

















                • 2




                  A good way to make heat.
                  – ctrl-alt-delor
                  20 hours ago










                • Delay-loops are a terrible idea for anything except the very shortest of sleeps (a couple nanoseconds or clock cycles in a device driver) on any modern CPU that can run a Unix-like OS. i.e. a sleep so short you can't usefully have the CPU do anything else while waiting, like schedule another process or enter a low-power sleep state before waking on a timer interrupt. Dynamic CPU-frequency makes it impossible to even calibrate a delay loop for counts per second, except as a minimum delay potentially sleeping a lot longer at low clock speeds before ramping up.
                  – Peter Cordes
                  12 hours ago










                • Ancient computers had a power consumption that was much less dependent on workload. Modern CPUs need to dynamically power down different parts of the chip as much as possible to not melt (e.g. power down parts of the FPU or SIMD execution units while only integer code is running, or at least gate the clock signal to parts of the chip that don't need to be switching). And entering a low-power sleep state when idle (instead of spinning in an infinite loop waiting for timer interrupts) is also more recent than the ancient computers you mention.
                  – Peter Cordes
                  12 hours ago










                • For more about CPU history, see Modern Microprocessors A 90-Minute Guide! - lighterra.com/papers/modernmicroprocessors.
                  – Peter Cordes
                  12 hours ago















                up vote
                2
                down vote













                Back in the days of microcomputers running BASIC, delays were usually accomplished with an empty loop:



                FOR I = 1 TO 10000:NEXT



                The same principle could be used to insert a delay in a shell script:



                COUNTER=0; while [ $COUNTER -lt 10000 ]; do :; let COUNTER=COUNTER+1; done



                Of course, the problem with this approach is that the length of the delay will vary from machine to machine according to its processor speed (or even on the same machine under different loads). Unlike sleep, it will probably also max out your CPU (or one of its cores).






                share|improve this answer

















                • 2




                  A good way to make heat.
                  – ctrl-alt-delor
                  20 hours ago










                • Delay-loops are a terrible idea for anything except the very shortest of sleeps (a couple nanoseconds or clock cycles in a device driver) on any modern CPU that can run a Unix-like OS. i.e. a sleep so short you can't usefully have the CPU do anything else while waiting, like schedule another process or enter a low-power sleep state before waking on a timer interrupt. Dynamic CPU-frequency makes it impossible to even calibrate a delay loop for counts per second, except as a minimum delay potentially sleeping a lot longer at low clock speeds before ramping up.
                  – Peter Cordes
                  12 hours ago










                • Ancient computers had a power consumption that was much less dependent on workload. Modern CPUs need to dynamically power down different parts of the chip as much as possible to not melt (e.g. power down parts of the FPU or SIMD execution units while only integer code is running, or at least gate the clock signal to parts of the chip that don't need to be switching). And entering a low-power sleep state when idle (instead of spinning in an infinite loop waiting for timer interrupts) is also more recent than the ancient computers you mention.
                  – Peter Cordes
                  12 hours ago










                • For more about CPU history, see Modern Microprocessors A 90-Minute Guide! - lighterra.com/papers/modernmicroprocessors.
                  – Peter Cordes
                  12 hours ago













                up vote
                2
                down vote










                up vote
                2
                down vote









                Back in the days of microcomputers running BASIC, delays were usually accomplished with an empty loop:



                FOR I = 1 TO 10000:NEXT



                The same principle could be used to insert a delay in a shell script:



                COUNTER=0; while [ $COUNTER -lt 10000 ]; do :; let COUNTER=COUNTER+1; done



                Of course, the problem with this approach is that the length of the delay will vary from machine to machine according to its processor speed (or even on the same machine under different loads). Unlike sleep, it will probably also max out your CPU (or one of its cores).






                share|improve this answer












                Back in the days of microcomputers running BASIC, delays were usually accomplished with an empty loop:



                FOR I = 1 TO 10000:NEXT



                The same principle could be used to insert a delay in a shell script:



                COUNTER=0; while [ $COUNTER -lt 10000 ]; do :; let COUNTER=COUNTER+1; done



                Of course, the problem with this approach is that the length of the delay will vary from machine to machine according to its processor speed (or even on the same machine under different loads). Unlike sleep, it will probably also max out your CPU (or one of its cores).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 22 hours ago









                Psychonaut

                314212




                314212








                • 2




                  A good way to make heat.
                  – ctrl-alt-delor
                  20 hours ago










                • Delay-loops are a terrible idea for anything except the very shortest of sleeps (a couple nanoseconds or clock cycles in a device driver) on any modern CPU that can run a Unix-like OS. i.e. a sleep so short you can't usefully have the CPU do anything else while waiting, like schedule another process or enter a low-power sleep state before waking on a timer interrupt. Dynamic CPU-frequency makes it impossible to even calibrate a delay loop for counts per second, except as a minimum delay potentially sleeping a lot longer at low clock speeds before ramping up.
                  – Peter Cordes
                  12 hours ago










                • Ancient computers had a power consumption that was much less dependent on workload. Modern CPUs need to dynamically power down different parts of the chip as much as possible to not melt (e.g. power down parts of the FPU or SIMD execution units while only integer code is running, or at least gate the clock signal to parts of the chip that don't need to be switching). And entering a low-power sleep state when idle (instead of spinning in an infinite loop waiting for timer interrupts) is also more recent than the ancient computers you mention.
                  – Peter Cordes
                  12 hours ago










                • For more about CPU history, see Modern Microprocessors A 90-Minute Guide! - lighterra.com/papers/modernmicroprocessors.
                  – Peter Cordes
                  12 hours ago














                • 2




                  A good way to make heat.
                  – ctrl-alt-delor
                  20 hours ago










                • Delay-loops are a terrible idea for anything except the very shortest of sleeps (a couple nanoseconds or clock cycles in a device driver) on any modern CPU that can run a Unix-like OS. i.e. a sleep so short you can't usefully have the CPU do anything else while waiting, like schedule another process or enter a low-power sleep state before waking on a timer interrupt. Dynamic CPU-frequency makes it impossible to even calibrate a delay loop for counts per second, except as a minimum delay potentially sleeping a lot longer at low clock speeds before ramping up.
                  – Peter Cordes
                  12 hours ago










                • Ancient computers had a power consumption that was much less dependent on workload. Modern CPUs need to dynamically power down different parts of the chip as much as possible to not melt (e.g. power down parts of the FPU or SIMD execution units while only integer code is running, or at least gate the clock signal to parts of the chip that don't need to be switching). And entering a low-power sleep state when idle (instead of spinning in an infinite loop waiting for timer interrupts) is also more recent than the ancient computers you mention.
                  – Peter Cordes
                  12 hours ago










                • For more about CPU history, see Modern Microprocessors A 90-Minute Guide! - lighterra.com/papers/modernmicroprocessors.
                  – Peter Cordes
                  12 hours ago








                2




                2




                A good way to make heat.
                – ctrl-alt-delor
                20 hours ago




                A good way to make heat.
                – ctrl-alt-delor
                20 hours ago












                Delay-loops are a terrible idea for anything except the very shortest of sleeps (a couple nanoseconds or clock cycles in a device driver) on any modern CPU that can run a Unix-like OS. i.e. a sleep so short you can't usefully have the CPU do anything else while waiting, like schedule another process or enter a low-power sleep state before waking on a timer interrupt. Dynamic CPU-frequency makes it impossible to even calibrate a delay loop for counts per second, except as a minimum delay potentially sleeping a lot longer at low clock speeds before ramping up.
                – Peter Cordes
                12 hours ago




                Delay-loops are a terrible idea for anything except the very shortest of sleeps (a couple nanoseconds or clock cycles in a device driver) on any modern CPU that can run a Unix-like OS. i.e. a sleep so short you can't usefully have the CPU do anything else while waiting, like schedule another process or enter a low-power sleep state before waking on a timer interrupt. Dynamic CPU-frequency makes it impossible to even calibrate a delay loop for counts per second, except as a minimum delay potentially sleeping a lot longer at low clock speeds before ramping up.
                – Peter Cordes
                12 hours ago












                Ancient computers had a power consumption that was much less dependent on workload. Modern CPUs need to dynamically power down different parts of the chip as much as possible to not melt (e.g. power down parts of the FPU or SIMD execution units while only integer code is running, or at least gate the clock signal to parts of the chip that don't need to be switching). And entering a low-power sleep state when idle (instead of spinning in an infinite loop waiting for timer interrupts) is also more recent than the ancient computers you mention.
                – Peter Cordes
                12 hours ago




                Ancient computers had a power consumption that was much less dependent on workload. Modern CPUs need to dynamically power down different parts of the chip as much as possible to not melt (e.g. power down parts of the FPU or SIMD execution units while only integer code is running, or at least gate the clock signal to parts of the chip that don't need to be switching). And entering a low-power sleep state when idle (instead of spinning in an infinite loop waiting for timer interrupts) is also more recent than the ancient computers you mention.
                – Peter Cordes
                12 hours ago












                For more about CPU history, see Modern Microprocessors A 90-Minute Guide! - lighterra.com/papers/modernmicroprocessors.
                – Peter Cordes
                12 hours ago




                For more about CPU history, see Modern Microprocessors A 90-Minute Guide! - lighterra.com/papers/modernmicroprocessors.
                – Peter Cordes
                12 hours ago










                up vote
                1
                down vote













                Some other ideas.



                top -d10 -n2 >/dev/null

                vmstat 10 2 >/dev/null

                sar 10 1 >/dev/null

                timeout 10s tail -f /dev/null





                share|improve this answer

























                  up vote
                  1
                  down vote













                  Some other ideas.



                  top -d10 -n2 >/dev/null

                  vmstat 10 2 >/dev/null

                  sar 10 1 >/dev/null

                  timeout 10s tail -f /dev/null





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Some other ideas.



                    top -d10 -n2 >/dev/null

                    vmstat 10 2 >/dev/null

                    sar 10 1 >/dev/null

                    timeout 10s tail -f /dev/null





                    share|improve this answer












                    Some other ideas.



                    top -d10 -n2 >/dev/null

                    vmstat 10 2 >/dev/null

                    sar 10 1 >/dev/null

                    timeout 10s tail -f /dev/null






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 12 hours ago









                    steve

                    13.6k22452




                    13.6k22452






















                        up vote
                        0
                        down vote













                        A classic from the Land of Windows and Batches:



                        ping -c 11 localhost >/dev/null && echo "This is a test"





                        share|improve this answer








                        New contributor




                        Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






















                          up vote
                          0
                          down vote













                          A classic from the Land of Windows and Batches:



                          ping -c 11 localhost >/dev/null && echo "This is a test"





                          share|improve this answer








                          New contributor




                          Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.




















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            A classic from the Land of Windows and Batches:



                            ping -c 11 localhost >/dev/null && echo "This is a test"





                            share|improve this answer








                            New contributor




                            Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            A classic from the Land of Windows and Batches:



                            ping -c 11 localhost >/dev/null && echo "This is a test"






                            share|improve this answer








                            New contributor




                            Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            share|improve this answer



                            share|improve this answer






                            New contributor




                            Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 2 hours ago









                            Joker_vD

                            101




                            101




                            New contributor




                            Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            Joker_vD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






















                                user321697 is a new contributor. Be nice, and check out our Code of Conduct.










                                 

                                draft saved


                                draft discarded


















                                user321697 is a new contributor. Be nice, and check out our Code of Conduct.













                                user321697 is a new contributor. Be nice, and check out our Code of Conduct.












                                user321697 is a new contributor. Be nice, and check out our Code of Conduct.















                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482725%2fshell-is-it-possible-to-delay-a-command-without-using-sleep%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                SQL update select statement

                                'app-layout' is not a known element: how to share Component with different Modules