使用shell脚本并行执行多个脚本

时间:2021-11-13 04:08:36

Usually I start two different gearman worker in different terminals using python command, once those gearman workers get ready, I execute client file which send request to both listening workers.

通常我使用python命令在不同的终端启动两个不同的齿轮工人,一旦那些齿轮工人准备就绪,我执行客户端文件,向两个听力工作者发送请求。

I was trying to put all three command in shellscript and execute them in one go, but could not

我试图将所有三个命令放在一个shellcript中并一次执行它们,但不能

#!/bin/sh
python /root/Desktop/karim/temp_git/ATD_final/ATD_allClass/classifier_allClass.py
python /root/Desktop/karim/temp_git/ATD_final/ATD_lessClass/classifier_lessClass.py
python ans_type.py 1

which gives:

folders = [f for f in sorted(listdir(container_path))
OSError: [Errno 2] No such file or directory: '/root/Desktop/karim/temp_git/ATD_final/ATD_pylink/trec_data'

after that I tried to put this python command in .sh in same directory and executed shellscripts

之后,我尝试将此python命令放在.sh中的同一目录中并执行shellscripts

allClass.sh:

#!/bin/sh
python /root/Desktop/karim/temp_git/ATD_final/ATD_allClass/classifier_allClass.py

lessClass.sh

python /root/Desktop/karim/temp_git/ATD_final/ATD_lessClass/classifier_lessClass.py

and in final.sh:

并在final.sh:

#!/bin/sh
sh /root/Desktop/karim/temp_git/ATD_final/ATD_allClass/allClass.sh
sh /root/Desktop/karim/temp_git/ATD_final/ATD_lessClass/less_lessClass.py
python ans_type.py 1

final.sh

#!/bin/sh
sh /root/Desktop/karim/temp_git/ATD_final/ATD_allClass/allClass.sh
sh /root/Desktop/karim/temp_git/ATD_final/ATD_lessClass/less_lessClass.py
python ans_type.py 1

which gives:

sh: 0: Can't open /root/Desktop/karim/temp_git/ATD_final/ATD_allClass/allClass.sh
link-grammar: Info: Dictionary found at /usr/share/link-grammar/en/4.0.dict
Traceback (most recent call last):

1 个解决方案

#1


0  

I think, your problem is that the workers are being executed one after another and not at the same time. When you start all three python-scripts in different terminals as before, they are executed concurrently. You might want to try starting the three scripts in one bash-script but with three different subshells. i.e.:

我认为,你的问题是工人一个接一个地被执行而不是同时被执行。当你像以前一样在不同的终端启动所有三个python脚本时,它们会同时执行。您可能想尝试在一个bash脚本中启动三个脚本,但是有三个不同的子shell。即:

( python path/to/first/worker/FirstWorker.py )
( python path/to/second/worker/SecondWorker.py )
( python path/to/client/Client.py )

According to http://www.tldp.org/LDP/abs/html/subshells.html , you call a subshell like that. Maybe it would even be helpful to start all of them in the background with & like so:

根据http://www.tldp.org/LDP/abs/html/subshel​​ls.html,您可以像这样调用子shell。也许在背景中使用&像这样开始所有这些都是有帮助的:

( python path/to/first/worker/FirstWorker.py ) &
( python path/to/second/worker/SecondWorker.py ) &
( python path/to/client/Client.py ) &

You can even redirect the workers' output but I am not exactly sure how to do that... Hope it helps...

你甚至可以重定向工人的输出,但我不确定如何做到这一点...希望它有帮助......

#1


0  

I think, your problem is that the workers are being executed one after another and not at the same time. When you start all three python-scripts in different terminals as before, they are executed concurrently. You might want to try starting the three scripts in one bash-script but with three different subshells. i.e.:

我认为,你的问题是工人一个接一个地被执行而不是同时被执行。当你像以前一样在不同的终端启动所有三个python脚本时,它们会同时执行。您可能想尝试在一个bash脚本中启动三个脚本,但是有三个不同的子shell。即:

( python path/to/first/worker/FirstWorker.py )
( python path/to/second/worker/SecondWorker.py )
( python path/to/client/Client.py )

According to http://www.tldp.org/LDP/abs/html/subshells.html , you call a subshell like that. Maybe it would even be helpful to start all of them in the background with & like so:

根据http://www.tldp.org/LDP/abs/html/subshel​​ls.html,您可以像这样调用子shell。也许在背景中使用&像这样开始所有这些都是有帮助的:

( python path/to/first/worker/FirstWorker.py ) &
( python path/to/second/worker/SecondWorker.py ) &
( python path/to/client/Client.py ) &

You can even redirect the workers' output but I am not exactly sure how to do that... Hope it helps...

你甚至可以重定向工人的输出,但我不确定如何做到这一点...希望它有帮助......