25

How can I run a command after X seconds without sleep. Or with sleep but without the shell just wait for the response of that command?

I used this but it didn't work

sleep 5 ; ls > a.txt

I need to run it in background. I try not to have to run it in a script I try to run ls after 5 seconds, and shell does not just wait for the end of the sleep

3
  • Why does it not work without the shell? Which error do you get? Are you passing it the full path? Do you know which part parses the ; ?
    – Hennes
    Commented Apr 4, 2013 at 17:30
  • in this way the shell will block 5 seconds and then run the command., the shell to be stalled I do not need that.
    – Martin
    Commented Apr 4, 2013 at 17:37
  • Then you either need to start it in a script in the background, or in a sub-shell.
    – Hennes
    Commented Apr 4, 2013 at 17:39

3 Answers 3

45

A more concise way of writing what Hennes suggested is

(sleep 5; echo foo) & 

Alternatively, if you need more than a few seconds, you could use at. There are three ways of giving a command to at:

  1. Pipe it:

    $ echo "ls > a.txt" | at now + 1 min
    warning: commands will be executed using /bin/sh
    job 3 at Thu Apr  4 20:16:00 2013
    
  2. Save the command you want to run in a text file, and then pass that file to at:

    $ echo "ls > a.txt" > cmd.txt
    $ at now + 1 min < cmd.txt
    warning: commands will be executed using /bin/sh
    job 3 at Thu Apr  4 20:16:00 2013
    
  3. You can also pass at commands from STDIN:

    $ at now + 1 min
    warning: commands will be executed using /bin/sh
    at> ls
    

    Then, press CtrlD to exit the at shell. The ls command will be run in one minute.

You can give very precise times in the format of [[CC]YY]MMDDhhmm[.ss], as in

$ at -t 201412182134.12 < script.sh

This will run the script script.sh at 21:34 and 12 seconds on the 18th of December 2014. So, in theory, you could use at to run something five seconds in the future. However, that is kinda like using a tank to swat a fly, and Hennes's suggestion is better.

4
  • I kept messing with $(sleep 5; echo foo) & I am not sure why I thought I needed a $ in front of that, but with my syntax wrong I just posted the longer (and more understandable) version. ( command_for_in_new_shell ) is shorter though.
    – Hennes
    Commented Apr 4, 2013 at 19:23
  • @Hennes and I blatantly ripped off your very good idea (which I upvoted). All I did was reformat it to be shorter, all credit for the actual thinking bits is yours :).
    – terdon
    Commented Apr 5, 2013 at 14:02
  • Is there any way to use at with a delay in seconds?
    – anon
    Commented Mar 18, 2019 at 3:48
  • @NicHartley no. Use a specific time (e.g. at 21:43) if that's what you want, or just do something simple like sleep 4 && command to run command after 4 seconds.
    – terdon
    Commented Mar 18, 2019 at 9:19
5

You can work around it by starting a shell or a script in the background.

Example:

/bin/sh -c "sleep 5 ; echo foo" &
[1] 63791
>                               Active shell prompt here

foo                             5 second later output appears.
0

This is how you would start apache web server after 4 hours. I fully understand that you want it without the command sleep. But I used a different approach about keeping the command running even after logging-out using the command nohup. Don't forget to add the & character at the end. It's part of the whole command below. It helps in making the command run detached from the command line prompt.

nohup sleep 14400 && service apache2 start &

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .