简体   繁体   English

如何在 bash 中在一行中运行多个后台命令?

[英]How do I run multiple background commands in bash in a single line?

I normally run multiple commands with something like this:我通常使用以下命令运行多个命令:

sleep 2 && sleep 3

or或者

sleep 2 ; sleep 3

but what if I want to run them both in the background from one command line command?但是如果我想从一个命令行命令在后台同时运行它们怎么办?

sleep 2 & && sleep 3 &

doesn't work.不起作用。 And neither does replacing && with ;;替换&&也不行

Is there a way to do it?有没有办法做到这一点?

Exactly how do you want them to run?您究竟希望它们如何运行? If you want them to be started in the background and run sequentially , you would do something like this:如果您希望它们在后台启动并按顺序运行,您可以执行以下操作:

{ sleep 2; sleep 3; } &

If you want sleep 3 to run only if sleep 2 succeeds, then:如果您希望sleep 3仅在sleep 2成功时运行,则:

sleep 2 && sleep 3 &

If, on the other hand, you would like them to run in parallel in the background , you can instead do this:另一方面,如果您希望它们在后台并行运行,您可以这样做:

sleep 2 & sleep 3 &

And the two techniques could be combined, such as:这两种技术可以结合使用,例如:

{ sleep 2; echo first finished; } & { sleep 3; echo second finished; } &

Bash being bash, there's often a multitude of different techniques to accomplish the same task, although sometimes with subtle differences between them. Bash 就是 bash,通常有多种不同的技术来完成相同的任务,尽管有时它们之间存在细微的差异。

You need to add some parens in your last version --您需要在上一个版本中添加一些括号 -

(sleep 2 &) && (sleep 3 &)

or this also works --或者这也有效——

(sleep 2 &) ; (sleep 3 &)

to run multiple background command you need to add & end of each command.要运行多个后台命令,您需要在每个命令的末尾添加& ex: (command1 &) && (command2 &) && (command3 &)例如: (command1 &) && (command2 &) && (command3 &)

The answers above use parentheses.上面的答案使用括号。 Bash also can use braces for a similar purpose: Bash 也可以将大括号用于类似目的:

{ sleep 2 && sleep 3; } &

Note that the braces are more picky about syntax--the space after { , the space before } , and the final semicolon are required.请注意,大括号对语法更加挑剔—— {之后的空格、 }之前的空格和最后的分号是必需的。 In some situations the braces are more efficient because they don't fork a new subshell.在某些情况下,大括号更有效,因为它们不会派生新的子外壳。 In this case I don't know if it makes a difference.在这种情况下,我不知道它是否有所作为。

You can use the bash command substitution $(command) like this:您可以像这样使用 bash 命令替换$(command)

$(command1 ; command2) &

Note that stdin and stdout are still linked to the parent process and redirecting at least the stdout can be tricky.请注意,stdin 和 stdout 仍然链接到父进程,并且至少重定向 stdout 可能会很棘手。 So alternatively you can chain the commands in a single line then pass the string to the bash command to spawn a new process which will handle the execution.因此,或者您可以将命令链接在一行中,然后将字符串传递给bash命令以生成一个新进程来处理执行。

bash -c "command1 ; command2" & 

This is especially useful in a bash script when you need to run multiple commands in background.当您需要在后台运行多个命令时,这在 bash 脚本中特别有用。

This two statements should be equivalent.这两个语句应该是等价的。 A bash process is spawn in both cases to handle the command (chain of commands) and the & at the end detaches the execution.在这两种情况下都会产生一个bash进程来处理命令(命令链),最后的&分离执行。

This time you can add &>/dev/null before the & at the end of the command to redirect at least the stdout and avoid the output on the stdout of the parent process.这次可以在命令末尾的&之前添加&>/dev/null以至少重定向 stdout 并避免在父进程的 stdout 上输出。 Something like:就像是:

bash -c "command1 ; command2" &>/dev/null &

This works:这有效:

$(sleep 2 &) && sleep 3 &

Also you can do:你也可以这样做:

$(sleep 2 && sleep 3) &

I have the same mission too.我也有同样的使命。 I have try (sleep2 ; fg )& sleep3 ; fg我试过(sleep2 ; fg )& sleep3 ; fg (sleep2 ; fg )& sleep3 ; fg ,it's working. (sleep2 ; fg )& sleep3 ; fg ,它正在工作。 And when you preass ctrl+c doubled,the two process can be stoppped.并且当你按 ctrl+c 加倍时,可以停止这两个过程。

If you want to run multiple commands sequentially , there is a really simple method that works everywhere: Creating a file!如果您想按顺序运行多个命令,有一个非常简单的方法适用于任何地方:创建文件! The benefit of this method is that it's clean and simple.这种方法的好处是干净简单。

First, create your file with a name, eg commands.sh .首先,使用名称创建文件,例如commands.sh Then, put your commands there.然后,把你的命令放在那里。 Here's is a sample:这是一个示例:

commands.sh:

#!/system/bin/sh

sleep 3;
sleep 2;

OK, now we're in the last step.好的,现在我们到了最后一步。 To run commands in the background (sequentially), just run:要在后台(按顺序)运行命令,只需运行:

$ sh commands.sh &

Cross-Platform Solution using NPM - but not "pure bash"使用 NPM 的跨平台解决方案 - 但不是“纯 bash”

If you already have node/npm on your machine, you can grab either of the following packages to assist in declaratively running multiple synchronous or asynchronous commands in a cross-platform friendly way:如果你的机器上已经有 node/npm,你可以获取以下任一包来帮助以跨平台友好的方式声明性地运行多个同步或异步命令:

Run multiple commands from any shell like this:从任何 shell 运行多个命令,如下所示:

npx concurrently "sleep 2" "sleep 5"

跨平台并发

This is a good escape hatch when you have to share scripts across machines and can't control where they'll be executed.当您必须跨机器共享脚本并且无法控制它们将在哪里执行时,这是一个很好的逃生舱口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM