我想你想要这个:
myCommand1 & myCommand2 &
这将启动myCommand1
,并将其发送到背景,然后再跟“&”号,然后立即启动myCommand2
并将其也发送到背景,因此再次释放外壳。
清单
为了更好地理解,您可以在此处通过命令替换管道。
列表是由一个运算符分隔的一个或多个流水线的序列; ,&,&&或|| ,并可选地由以下之一终止
; ,&或 。
如果命令由控制操作符&终止,则外壳程序将在子外壳程序的后台执行该命令。外壳程序不等待命令完成,返回状态为0 。按顺序执行;Shell等待每个命令依次终止。返回状态是最后执行的命令的退出状态。
AND和OR列表是由&& 和 ||分隔的一个或多个管道的序列。 分别控制操作员。
资源:man bash
让我们将其分解为示例。您可以通过组合命令并将命令与以下命令之一分开来构建列表; & && ||
:
command1 ; command2 # runs sequentially
command1 && command2 # runs sequentially, runs command2 only if command1 succeeds
command1 || command2 # runs sequentially, runs command2 only if command1 fails
command1 & command2 # runs simultaneously
您可以使用以下其中一项终止列表:; & <newline>
。
通常,您通过按Enter等于来执行命令或列表<newline>
。分号;
具有相同的目的,尤其是在脚本中。符号&
然而在背景子shell启动该命令(S),立即释放外壳。
您可以使用()
圆括号或大括号{}
将列表进一步分组,不同之处在于圆括号会生成子外壳,而大括号则不会。弯括号在第一个括号后需要一个空格,在结束括号之前需要一个分号或换行符。例如:
# if c1 succeeds start a shell in the background
# and run c2 and c3 sequentially inside it
c1 && ( c2 ; c3 ) &
# run c1 and if it succeeds c2 sequentially as a group command
# if c1 or c2 fail run c3 in the background
{ c1 && c2 ;} || c3 &
如果您不确定使用true
并false
测试构造是否按预期工作,这可能会变得非常复杂:
$ { true && true ;} || echo 2
$ { true && false ;} || echo 2
2
工作控制
该jobs
命令显示当前外壳中正在运行或最近已完成的后台作业的列表。有许多键盘快捷键和命令可用于作业控制:
- Ctrl+ Z键入使当前在前台运行的进程停止的挂起字符,它不会终止,而是保留在
jobs
列表中
- Ctrl+ Y键入延迟的挂起字符,当尝试从终端读取输入时,导致当前在前台运行的进程被停止
fg
= %
如有必要,可将某个进程置于前台,您可以按以下方式指定该进程:
% # last process in the jobs list
%1 # 1st process in the jobs list
%abc # process beginning with the string “abc”
%?abc # process containing the string “abc” anywhere
bg
= %&
在必要时将进程引入后台:
%& # last process in the jobs list
%1& # 1st process in the jobs list
%abc& # process beginning with the string “abc”
%?abc& # process containing the string “abc” anywhere
wait
等待后台进程完成并返回其终止状态:
wait %1 # 1st process in the jobs list
想象一下,您开始了一个漫长的过程(jobs
显示为3),然后意识到您希望计算机在完成时被挂起,echo
如果过程没有成功,还会显示一条消息:
wait %3 || echo failed ; systemctl suspend