如何在后台启动和运行外部脚本? [英] How to launch and run external script in background?

查看:117
本文介绍了如何在后台启动和运行外部脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这两种方法:

os.system("python test.py")

subprocess.Popen("python test.py", shell=True)

这两种方法都需要等到test.py完成后才阻塞主进程.我知道"nohup"可以胜任.有没有Python方法来启动test.py或任何其他Shell脚本并使它在后台运行?

Both approaches need to wait until test.py finishes which blocks main process. I know "nohup" can do the job. Is there a Python way to launch test.py or any other shell scripts and leave it running in background?

假设test.py是这样的:

Suppose test.py is like this:

for i in range(0, 1000000):
    print i

os.system()或subprocess.Popen()都将阻塞主程序,直到显示1000000行输出为止.我想要的是让test.py静默运行并仅显示主程序输出.当test.py仍在运行时,主程序可能会退出.

Both os.system() or subprocess.Popen() will block main program until 1000000 lines of output displayed. What I want is let test.py runs silently and display main program output only. Main program may quie while test.py is still running.

推荐答案

subprocess.Popen(["python", "test.py"]) 应该可以工作.

请注意,当您的主脚本退出时,该作业可能仍然会死.在这种情况下,请尝试subprocess.Popen(["nohup", "python", "test.py"])

Note that the job might still die when your main script exits. In this case, try subprocess.Popen(["nohup", "python", "test.py"])

这篇关于如何在后台启动和运行外部脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆