我有两个命令,我正在执行shell command1
和command2
。 command1需要很长时间才能完成(约2分钟)。所以我可以使用&
让它在后台运行,但之后我想自动执行command2。我可以在shell命令行上执行此操作吗?
尝试:
( command1; command2 ) &
编辑:更大的PoC:
demo.是:
#!/bin/bash
echo Starting at $(date)
( echo Starting background process at $(date); sleep 5; echo Ending background process at $(date) ) &
echo Last command in script at $(date)
运行demo.sh:
$ ./demo.sh
Starting at Thu Mar 1 09:11:04 MST 2018
Starting background process at Thu Mar 1 09:11:04 MST 2018
Last command in script at Thu Mar 1 09:11:04 MST 2018
$ Ending background process at Thu Mar 1 09:11:09 MST 2018
请注意,脚本在“脚本中的最后一个命令”之后结束,但后台进程在5秒后完成了“结束后台进程”回显。 (...)和结构中的所有命令都是串行运行的,但都是统一分配到后台的。
您可以通过将两个赞誉放在shell脚本中来执行此操作,如下所示: -
command1 &
wait
command2 & #if you want to run second command also in background