繁体   English   中英

从作为守护程序运行的另一个python脚本运行python脚本

[英]Run a python script from another python script running as a daemon

我有一个小的python文件,它仅输出一个字符串:

#!/usr/bin/env python
print("This is a Test")

我可以像这样从另一个python脚本调用此python脚本:

subprocess.call(['python', 'myPythonFile.py'])

而且我可以在源python程序中看到“这是一个测试”。

但我想按如下所述从正在运行的守护程序中调用此脚本: https : //gist.github.com/andreif/cbb71b0498589dac93cb

当我打电话给

    subprocess.call(['python', 'myPythonFile.py'])

在MyDaemon.Run中,我看不到输出。

我怎样才能做到这一点?

尝试使用check_output函数在控制台中查看实际输出

subprocess.check_output(['python', 'myPythonFile.py'])

您可以在子流程文档中找到更多信息

subprocess.call可以将其输出发送到文件

tempfile = '/tmp/output.tmp' # better use mktemp

with open( tempfile, 'w') as output:
    response = subprocess.call(
         ['python', 'myPythonFile.py'], 
         stdout=output, 
         stderr=output,
    )
with open( tempfile, 'r') as output:
    # read what happened from output, and decide what to do next
    # or perhaps just feed it into your logging system

守护进程的特征是没有控制终端,因为它与启动守护进程的对象是分离的。 根据定义,守护进程未连接到任何控制台。

因此,如果该守护程序运行另一个进程:

我想从正在运行的守护程序中调用此脚本

则仍然没有控制端子,并且默认情况下标准输出连接到空设备。

您将需要安排守护进程以使其输出在某处。 例如,一个日志文件。

尝试使用python daemon来创建守护程序, 指定特定文件(例如,打开的日志文件)以在守护程序进程中保持打开状态。

暂无
暂无

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

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