views:

197

answers:

3

How can I spawn a process after a delay in a shell script? I want a command to start 60 seconds after the script starts, but I want to keep running the rest of the script without waiting 60 seconds first. Here's the idea:

#!/bin/sh
# Echo A 60 seconds later, but without blocking the rest of the script
sleep 60 && echo "A"

echo "B"
echo "C"

The output should be

B
C
... 60 seconds later
A

I need to be able to do this all in one script. Ie. no creating a second script that is called from the first shell script.

A: 

Can't try it right now but

(sleep 60 && echo "A")&

should do the job

lorenzog
+4  A: 

& starts a background job, so

sleep 60 && echo "A" &
nos
I was surprised to see that work without grouping (parentheses or braces). Thanks :-)
Tanktalus
+2  A: 

A slight expansion on the other answers is to wait for the backgrounded commands at the end of the script.

#!/bin/sh
# Echo A 60 seconds later, but without blocking the rest of the script

set -e

sleep 60 && echo "A" &
pid=$!

echo "B"
echo "C"

wait $pid
davrieb