How to Sleep 10 Seconds Before Running a Linux Command

How to sleep 10 seconds before running a linux command?

* * * * * sleep 10;curl http://www.google.com/

Linux bash sleep if the command execution is fast

You can run sleep 10 at the background and wait for it to complete after my_command.sh finishes.

sleep 10 & my_command.sh; wait

If there are other background jobs this will wait for them too, though. In that case you can either run it in a subshell, e.g:

( sleep 10 & my_command.sh; wait )

or keep sleep 10's PID in a variable and wait for it, thus my_command.sh will be run in current execution environment, e.g:

sleep 10 & pid=$!; my_command.sh; wait "$pid"

Bash script how to sleep in new process then execute a command

You can invoke another shell in the background and make it do what you want:

bash -c 'sleep 30; do-whatever-else' &

The default interval for sleep is in seconds, so the above would sleep for 30 seconds. You can specify other intervals like: 30m for 30 minutes, or 1h for 1 hour, or 3d for 3 days.

bash script: wait a few seconds since last run

The first question I would ask myself is, "Do I really need to delay the execution of the script for these commands?" If you can run multiple commands in parallel and then wait for each process to finish with the wait command, that is generally faster and preferable. Have a look at this SO question discussing the difference between wait and sleep

That said, you can do something like this if you really need to wait at least five seconds:

#!/bin/bash
export delay_seconds=5
export start_time=`date +%s`

#Run some commands here...

seconds_since_start=`expr \`date +%s\` - $start_time`

if [ $seconds_since_start -lt $delay_seconds ]; then
time_remaining=`expr $delay_seconds - $seconds_since_start`
echo "Sleeping "$time_remaining" seconds..."
sleep $time_remaining
else
echo $seconds_since_start" seconds have already passed since the commands above started. Continuing..."
fi

echo "Script finished."

Sleep until a specific time/date

As mentioned by Outlaw Programmer, I think the solution is just to sleep for the correct number of seconds.

To do this in bash, do the following:

current_epoch=$(date +%s)
target_epoch=$(date -d '01/01/2010 12:00' +%s)

sleep_seconds=$(( $target_epoch - $current_epoch ))

sleep $sleep_seconds

To add precision down to nanoseconds (effectively more around milliseconds) use e.g. this syntax:

current_epoch=$(date +%s.%N)
target_epoch=$(date -d "20:25:00.12345" +%s.%N)

sleep_seconds=$(echo "$target_epoch - $current_epoch"|bc)

sleep $sleep_seconds

Note that macOS / OS X does not support precision below seconds, you would need to use coreutils from brew instead → see these instructions

How to sleep for five seconds in a batch file/cmd

One hack is to (mis)use the ping command:

ping 127.0.0.1 -n 6 > nul

Explanation:

  • ping is a system utility that sends ping requests. ping is available on all versions of Windows.
  • 127.0.0.1 is the IP address of localhost. This IP address is guaranteed to always resolve, be reachable, and immediately respond to pings.
  • -n 6 specifies that there are to be 6 pings. There is a 1s delay between each ping, so for a 5s delay you need to send 6 pings.
  • > nul suppress the output of ping, by redirecting it to nul.


Related Topics



Leave a reply



Submit