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
Best Answer
A more concise way of writing what Hennes suggested is
Alternatively, if you need more than a few seconds, you could use
at
. There are three ways of giving a command toat
:Pipe it:
Save the command you want to run in a text file, and then pass that file to
at
:You can also pass
at
commands from STDIN:Then, press CtrlD to exit the
at
shell. Thels
command will be run in one minute.You can give very precise times in the format of
[[CC]YY]MMDDhhmm[.ss]
, as inThis will run the script
script.sh
at 21:34 and 12 seconds on the 18th of December 2014. So, in theory, you could useat
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.