Running a linux com...
 
Notifications
Clear all

Running a linux command from python  

  RSS

Abhijith
(@abhijith)
Noble Member
Joined: 4 months ago
Posts: 1161
01/04/2021 12:31 pm  

I just tried to run this Linux code from python and specify the output to a variable.

 

ps -ef | grep rtptransmit | grep -v grep

For this, I have used the Python commands library.

 

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'

However, 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


Quote
Ganesh A
(@ganesh)
Noble Member
Joined: 4 months ago
Posts: 1170
01/04/2021 12:32 pm  

Kindly be informed that the ps possibly limits its output to fit into the presumed width of the terminal. You can also override this width with the $COLUMNS context variable or with the --columns option to the ps.

 

The commands package is deprecated. So, it is better to use the subprocess to get the output of ps -ef and filter the output in Python:

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)


ReplyQuote
Share: