I need to run this linux command from python and assign the output to a variable.
ps -ef | grep rtptransmit | grep -v grep
I’ve tried using pythons commands library to do this.
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:
'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?
psapparently limits its output to fit into the presumed width of the terminal. You can override this width with the$COLUMNSenvironment variable or with the--columnsoption tops.The
commandsmodule is deprecated. Usesubprocessto get the output ofps -efand filter the output in Python. Do not useshell=Trueas suggested by other answers, it is simply superfluous in this case:You may also want to take a look the
pgrepcommand by which you can directly search for specific processes.