Running a linux command from python
I usually use subprocess
for running an external command. For your case, you can do something like the following
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.
commands
is deprecated, you should not use it. Use subprocess
instead
import subprocess
a = subprocess.check_output('ps -ef | grep rtptransmit | grep -v grep', shell=True)
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
.
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:
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.