abhi
abhi

Reputation: 9

usage of sleep with multiple commands

with what command we can start five commands in background each one sleeping for 10 seconds

Upvotes: 0

Views: 1128

Answers (2)

user1481860
user1481860

Reputation:

You mean like this? Sounds weird to me

sleep 10 &
sleep 10 &
sleep 10 &
sleep 10 &
sleep 10 &

Upvotes: 0

Joey Adams
Joey Adams

Reputation: 43380

In a shell, you could just do this:

#!/bin/sh
sleep 10 && command1 &
sleep 10 && command2 &
sleep 10 && command3 &
sleep 10 && command4 &
sleep 10 && command5 &

The & at the end tells the command to be backgrounded.

To turn this into a general-purpose script, you could do something like:

#!/bin/sh
sleep 10 && $1 &
sleep 10 && $2 &
sleep 10 && $3 &
sleep 10 && $4 &
sleep 10 && $5 &

Usage:

./script 'echo 1' 'echo 2' 'echo 3' 'echo 4' 'echo 5'

Note: you usually want to quote parameters passed to shell scripts (i.e. "$1") so spaces aren't split into words. Because I didn't do that, the arguments are split into words, so it becomes && echo 1 instead of && "echo 1". This works great for our trivial example, but not when you want to run '/usr/bin/command with spaces'

Upvotes: 1

Related Questions