从 python 运行 linux 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9735863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a linux command from python
提问by DarylF
I need to run this linux command from python and assign the output to a variable.
我需要从 python 运行这个 linux 命令并将输出分配给一个变量。
ps -ef | grep rtptransmit | grep -v grep
I've tried using pythons commands library to do this.
我试过使用 pythons 命令库来做到这一点。
import commands
a = commands.getoutput('ps -ef | grep rtptransmit | grep -v grep')
But a gets the end of cut off. The output I get is:
但是a得到了切断的结束。我得到的输出是:
'nvr 20714 20711 0 10:39 ? 00:00:00 /opt/americandynamics/venvr/bin/rtptransmit setup_req db=media camera=6 stream=video substream=1 client_a'
but the expected output is:
但预期的输出是:
nvr 20714 20711 0 10:39 ? 00:00:00 /opt/americandynamics/venvr/bin/rtptransmit setup_req db=media camera=6 stream=video substream=1 client_address=192.168.200.179 client_rtp_port=6970 override_lockout=1 clienttype=1
Does anyone know how to stop the output from getting cut off or can anyone suggest another method?
有谁知道如何阻止输出被切断或者有人可以建议另一种方法吗?
采纳答案by lunaryorn
ps
apparently limits its output to fit into the presumed width of the terminal. You can override this width with the $COLUMNS
environment variable or with the --columns
option to ps
.
ps
显然将其输出限制为适合终端的假定宽度。您可以使用$COLUMNS
环境变量或--columns
选项覆盖此宽度ps
。
The commands
module is deprecated. Use subprocess
to get the output of ps -ef
and filter the output in Python. Do not use shell=True
as suggested by other answers, it is simply superfluous in this case:
该commands
模块已弃用。用于在 Python 中subprocess
获取输出ps -ef
并过滤输出。不要shell=True
按照其他答案的建议使用,在这种情况下它只是多余的:
ps = subprocess.Popen(['ps', '-ef', '--columns', '1000'], stdout=subprocess.PIPE)
output = ps.communicate()[0]
for line in output.splitlines():
if 'rtptransmit' in line:
print(line)
You may also want to take a look the pgrep
command by which you can directly search for specific processes.
您可能还想查看pgrep
可以直接搜索特定进程的命令。
回答by fajran
I usually use subprocess
for running an external command. For your case, you can do something like the following
我通常subprocess
用于运行外部命令。对于您的情况,您可以执行以下操作
from subprocess import Popen, PIPE
p = Popen('ps -ef | grep rtptransmit | grep -v grep', shell=True,
stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
The output will be in out
variable.
输出将是out
可变的。
回答by vartec
commands
is deprecated, you should not use it. Use subprocess
instead
commands
已弃用,您不应使用它。使用subprocess
替代
import subprocess
a = subprocess.check_output('ps -ef | grep rtptransmit | grep -v grep', shell=True)
回答by sudhams reddy
#!/usr/bin/python
import os
a = os.system("cat /var/log/syslog")
print a
from subprocess import call
b = call("ls -l", shell=True)
print b
import subprocess
cmd = subprocess.check_output('ps -ef | grep kernel', shell=True)
print cmd
Any of the above script will work for you :-)
上述任何脚本都适合您:-)
回答by dru
nano test.py
import os
a = os.system('ps -ef | grep rtptransmit | grep -v grep')
print(a)
python test.py
python3 test.py
Run python file using both python and python3
使用 python 和 python3 运行 python 文件