简体   繁体   English

在后台在后台运行一个命令,在同一行在前台运行另一个命令

[英]Run a command in background and another command in frontground in the same line

I want to run 2 commands (command1 and command2) in the same line, where command1 starts a background process and command2 starts a frontground process. 我想在同一行中运行2个命令(command1和command2),其中command1启动一个后台进程,而command2启动一个前台进程。

I tried: 我试过了:

command1 & ; command2

But it says: "-bash: syntax error near unexpected token `;'" 但它说:“-bash:意外令牌';'附近的语法错误”

How could I run the 2 commands in the same line? 如何在同一行中运行这两个命令?

; is not helping here. 在这里没有帮助。 The control operator you need here is & after the first command (thanks Nick Russo in comments ): 您在这里需要的控制运算符是&在第一个命令之后(感谢Nick Russo在注释中 ):

command1 & command2

From man bash : 来自man bash

If a command is terminated by the control operator & , the shell executes the command in the background in a subshell. 如果命令由控制操作符&终止,则外壳程序将在子外壳程序的后台执行该命令。 The shell does not wait for the command to finish, and the return status is 0. Shell不等待命令完成,返回状态为0。

Commands separated by a ; 以分隔的命令; are executed sequentially; 按顺序执行; the shell waits for each command to terminate in turn. Shell等待每个命令依次终止。 The return status is the exit status of the last command executed. 返回状态是最后执行的命令的退出状态。

Test 测试

$ sleep 10 & echo "yes"
[2] 13368
yes
$ 
[1]-  Done                    sleep 10
$ 

Try this: 尝试这个:

(command1 &); command2

The the syntax (command) is creating a "subshell". 语法(command)正在创建“子外壳”。 You can read here something about it. 您可以在这里阅读有关它的内容。

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

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