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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14612371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:52:04  来源:igfitidea点击:

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

linuxbashshell

提问by Palace Chan

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?

有没有办法做到这一点?

采纳答案by Kitsune

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, on the other hand, you would like them to run in parallelin 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,通常有多种不同的技术来完成相同的任务,尽管有时它们之间存在细微的差异。

回答by higuaro

This works:

这有效:

$(sleep 2 &) && sleep 3 &

Also you can do:

你也可以这样做:

$(sleep 2 && sleep 3) &

回答by iagreen

You need to add some parens in your last version --

您需要在上一个版本中添加一些括号——

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

or this also works --

或者这也有效——

(sleep 2 &) ; (sleep 3 &)

回答by saeed

to run multiple background command you need to add &end of each command. ex: (command1 &) && (command2 &) && (command3 &)

要运行多个后台命令,您需要在&每个命令的末尾添加。前任: (command1 &) && (command2 &) && (command3 &)

回答by Greg Reagle

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.

请注意,大括号对语法更加挑剔——后面{的空格、前面的空格}和最后的分号是必需的。在某些情况下,大括号更有效,因为它们不会分叉新的子外壳。在这种情况下,我不知道它是否有所作为。

回答by dingyang wang

I have the same mission too. I have try (sleep2 ; fg )& sleep3 ; fg,it's working. And when you preass ctrl+c doubled,the two process can be stoppped.

我也有同样的使命。我试过了(sleep2 ; fg )& sleep3 ; fg,它的工作。当你按 ctrl+c 加倍时,这两个进程可以被停止。

回答by MAChitgarha

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, e.g. commands.sh. Then, put your commands there. Here's is a sample:

首先,使用名称创建文件,例如commands.sh. 然后,把你的命令放在那里。这是一个示例:

commands.sh:

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 &