简体   繁体   English

如何从命令行运行多个Python脚本?

[英]How to run multiple Python scripts from command line?

I have ten python scripts in the same directory. 我在同一目录中有十个python脚本。 How to run all of these from command line, that it will work in background? 如何从命令行运行所有这些,使其在后台运行?

I use SSH terminal to connect to server CentOS and run Python script as: 我使用SSH终端连接到服务器CentOS并以如下方式运行Python脚本:

python index.py

But when I close client terminal SSH, proccess is died 但是,当我关闭客户端SSH时,过程死亡

You can use the & command to make things run in the background, and nohup so it continues on logout, such as 您可以使用&命令使事物在后台运行,然后使用nohup使其在注销时继续运行,例如

nohup python index.py &

If you want to run multiple things this way, it's probably easiest to just make a script to start them all (with a shell of your choice): 如果您想以这种方式运行多件事,那么最简单的方法就是编写一个脚本来全部启动(使用您选择的shell):

#!/bin/bash
nohup python index1.py &
nohup python index2.py &
...

As long as you don't need to interact with the scripts once they are started (and don't need any stdout printing) this could be pretty easily automated with another python script using the subprocess module: 只要您不需要在脚本启动后就与它们进行交互(并且不需要任何标准输出),就可以使用subprocess模块​​使用另一个python脚本轻松地将其自动化:

for script in listofscripts:
    #use subprocess.run() for python 3.x (this blocks until each script terminates)
    subprocess.call(["python", script], *args) #use popen if you want non - blocking

*args is a link (it's coloring got overwritten by code highliting * args是一个链接(它的颜色被代码高亮覆盖

also of note: stdout / stderr printing is possible, just more work.. 还要注意:可以执行stdout / stderr打印,只需更多工作即可。

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

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