Shell Script – How to Delay a Command Without Using `sleep`

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 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

Related Question