在屏幕上并行和并行运行shell脚本

问题描述

我想在GNU屏幕的shell脚本中运行3个命令。

以下是script.sh中的内容

counter=$((counter + 1))
screen -dmS $counter sh -c \
"command1;exec bash;\
python3 script2.py ${word};exec bash &&\
command3;exec bash"

当我运行./script.sh时。我想先运行command1,然后运行python3 script2。我只想在python3 script2完成后运行command3。

我希望能够打开屏幕并检查python3 script2进程,直到完成为止,这就是我这样做的原因。

但是我可以看到命令1和命令3正在同时运行。

执行此代码的正确方法是什么?

解决方法

因为python3 script2.py $ {word}和exec bash之间有“,”。它将执行 exec bash ,而无需等待script2的完成。

exec bash 将立即完成,在 script2

完成之前调用命令3

我进行了以下更改

"command1 ; exec bash;\
python3 script2.py ${word} && exec bash ;\
command3;exec bash"