简体   繁体   English

没有标准输出的启动过程

[英]Start process without stdout output

I am using a python script as a bootstrapper to start a long running java service. 我使用python脚本作为引导程序来启动长时间运行的Java服务。

call = [ "java", "-cp", ... ]

with open(os.devnull, 'w') as fnull:
    subprocess.Popen(call, stdout=fnull)
    print "Service started."

But when the python process has finished, I am still getting stdout output from the java process. 但是当python进程完成时,我仍然从java进程中获得stdout输出。 I assume this is because python has shut my fnull so it reverts back to normal stdout. 我认为这是因为python关闭了我的fnull所以它恢复了正常的stdout。

How can I achieve what I want? 我怎样才能实现自己想要的?

EDIT: On a side note, it would be nice to still get stderr if at all possible, but not if it adds any complexity. 编辑:顺便说一句,如果可能的话仍然会得到stderr会很好,但是如果增加任何复杂性就不会。

EDIT2: It would appear that I am very much a moron, it was stderr that was being written to. EDIT2:看来我非常傻瓜,写的是stderr。

Partial answer to the side note, its easy to get stderr (its more complex when you want both stdout and stderr). 对旁注的部分回答,它很容易获得stderr(在同时需要stdout和stderr时更复杂)。 It has the added advantage that it will tell you whether the java output you think is on stdout is really on stderr. 它具有附加的优点,它将告诉您您认为在stdout上的java输出是否真的在stderr上。

call = [ "java", "-cp", ... ]

with open(os.devnull, 'w') as fnull:
    subprocess.Popen(call, stdout=fnull, stderr=subprocess.PIPE)
    print "Service started."
for line in stderr:
    print 'stderr: ', line.strip()

If you are okay with letting the python script live 如果您可以正常使用python脚本

Popen.communicate(input=None) Popen.communicate(输入=无)

Interact with process: Send data to stdin. 与进程交互:将数据发送到stdin。 Read data from stdout and stderr, until end-of-file is reached. 从stdout和stderr读取数据,直到到达文件末尾。 Wait for process to terminate. 等待进程终止。 The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child. 可选输入参数应该是要发送给子进程的字符串,如果没有数据要发送给子进程,则为None。

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

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