[英]python subprocess.check_output giving wrong output
I have problem execute this code here 我在这里执行此代码有问题
subprocess.check_output(['ps -ef | grep ftp | wc -l'],env=environ,shell=True)
When I execute from terminal 从终端执行时
ps -ef | grep ftp | wc -l
I get "1" as output which is fine. 我得到“ 1”作为输出,这很好。
Now, I execute same code from my python files as subprocess.check_output and it gives me 2. That is strange. 现在,我从python文件中执行与subprocess.check_output相同的代码,它给了我2。这很奇怪。 Any Ideas why is it happening.
任何想法为什么会发生。 Here is the complete code:
这是完整的代码:
def countFunction():
environ = dict(os.environ)
return subprocess.check_output(['ps -ef | grep ftp | wc -l'],env=environ,shell=True)
count = countFunction()
print count
EDIT: Just to update , I do not have any ftp connections on.So command line is printing 1 on command which is fine. 编辑:只是为了更新,我没有任何ftp连接。所以命令行在命令上打印1很好。 Thanks Arvind
感谢Arvind
The grep
command will find itself: grep
命令会发现自己:
$ ps -ef | grep ftp
wallyk 12546 12326 0 16:25 pts/3 00:00:00 grep ftp
If you don't want that, exclude the grep command: 如果您不想这样做,请排除grep命令:
$ ps -ef | grep ftp | grep -v ftp
$
It would be better to drop the -f
switch to ps
so that the command line arguments are not searched. 最好将
-f
开关放在ps
以便不搜索命令行参数。 That way, it won't find the grep ftp
running: 这样,它将找不到运行的
grep ftp
:
$ ps -e | grep ftp | wc -l
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.