简体   繁体   English

python子进程输出被截断

[英]python subprocess output truncated

While running shell command in python using subprocess I am getting wrong output. 使用子进程在python中运行shell命令时,输出错误。

grep ( com.vertica.solutions.kafka.Launcher ) in bash shell bash shell中的grep( com.vertica.solutions.kafka.Launcher

ps -ef | grep com.vertica.solutions.kafka.Launcher
root      92300  39024  0 23:06 pts/1    00:00:00 grep --color=auto  com.vertica.solutions.kafka.Launcher
dbadmin  413872 413868  0 22:06 pts/0    00:00:24 java -cp /opt/vertica/packages/kafka/bin/../lib/*:/opt/vertica/java/lib/vertica-jdbc.jar:/opt/vertica/packages/kafka/bin/../config/* -Dlog4j.configurationFile=/opt/vertica/packages/kafka/bin/../config/vkafka-log-config.xml -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector com.vertica.solutions.kafka.Launcher --conf /home/dbadmin/live.conf

grep ( com.vertica.solutions.kafka.Launcher ) in python shell python shell中的grep( com.vertica.solutions.kafka.Launcher

>>> cmd = "ps aux |grep com.vertica.solutions.kafka.Launcher| grep -v grep"
>>> check_output(cmd,shell=True).strip()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'ps aux |grep com.vertica.solutions.kafka.Launcher| grep -v grep' returned non-zero exit status 1

This is because subprocess is truncating ps output. 这是因为子进程正在截断ps输出。 As when I grep with word kafka I get the result but its not desired. 当我用单词kafka grep时,我得到了结果,但不是所希望的。

grep only kafka grep only kafka

>>> cmd = "ps aux | grep kafka | grep -v grep"
>>> check_output(cmd,shell=True).strip()
dbadmin  413868  0.0  0.0 113128  1372 pts/0    S    22:06   0:00   /bin/bash /opt/vertica/packages/kafka/bin/vkconfig launch --conf /home/dbadmin/live.conf\n
dbadmin  413872  0.6  1.0 7403000 175544 pts/0  Sl   22:06   0:25 java -cp /opt/vertica/packages/kafka/bin/../lib/*:/opt/vertica/java/lib/vertica-jdbc.jar:/opt/vertica/package'

When I grep only with word kafka I get the process (pid 413872) but in python shell COMMAND column output of process command is incomplete. 当我仅使用单词kafka grep时,我得到了进程(pid 413872),但是在python shell中,process命令的COMMAND列输出不完整。

Subprocess isn't truncating whatever get's written into stdout , it is piped to your python session. 子进程不会truncating任何写入stdout ,而是通过管道传递到您的python会话。 What happens is that -v isn't successful so it exits with a bad return code (None Zero). 发生的情况是-v不成功,因此它以错误的返回码(无零)退出。

To ignore the cases where you don't get any output from grep you can do the following: 要忽略无法从grep获得任何输出的情况,可以执行以下操作:

import os
from subprocess import Popen

DEVNULL = open(os.devnull, 'wb')
cmd = 'ps aux |grep com.vertica.solutions.kafka.Launcher| grep -v grep' 
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=DEVNULL, shell=True)
print process.communicate()[0] #output 

That will make sure, in case the grep isn't successful, instead of returning an error code, it will just dump it. 这可以确保万一grep不成功,而不是返回错误代码,而是将其转储。

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

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