开发者

usage of sleep with multiple commands

with what command we can s开发者_运维百科tart five commands in background each one sleeping for 10 seconds


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'


You mean like this? Sounds weird to me

sleep 10 &
sleep 10 &
sleep 10 &
sleep 10 &
sleep 10 &
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜