私は通常 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()
出力は out になります
commands は推奨されていないため、使用しないでください。 subprocess を使用 代わりに
import subprocess
a = subprocess.check_output('ps -ef | grep rtptransmit | grep -v grep', shell=True)
ps 端末の推定幅に収まるように出力を明らかに制限します。この幅は $COLUMNS で上書きできます 環境変数または --columns を使用 ps へのオプション .
commands モジュールは非推奨です。 subprocess を使用 ps -ef の出力を取得するには Python で出力をフィルタリングします。 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)
pgrep も参照してください。 特定のプロセスを直接検索できるコマンド。