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"
bashlinuxshellshell-scriptsleep
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"
Best Answer
You have alternatives to
sleep
: They areat
andcron
. Contrary tosleep
these need you to provide the time at which you need them to run.Make sure the
atd
service is running by executingservice 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 orcrontab -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 is12:30:30
and now you ran the scheduler withnow +1 minutes
. Even though 1 minute, which translates to 60 seconds was specified , theat
doesn't really wait till12:31:30
to execute the job, rather it executes the job at12:31:00
. The time-units can beminutes, hours, days, or weeks
. For more referman at
e.g:
echo "ls" | at now +1 minutes