开发者

usage of sleep with multiple commands

开发者 https://www.devze.com 2023-01-29 12:45 出处:网络
with what command we can s开发者_运维百科tart five commands in background each one sleeping for 10 secondsIn a shell, you could just do this:

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

精彩评论

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