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?
Introduction. Python has a rich set of libraries that allow us to execute shell commands. The os. system() function allows users to execute commands in Python.
You cannot use UNIX commands in your Python script as if they were Python code, echo name is causing a syntax error because echo is not a built-in statement or function in Python. Instead, use print name . To run UNIX commands you will need to create a subprocess that runs the command.
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!
Run Multiple Commands at Once In Linux: You can use the | operator to concatenate two commands. The first command will list the files and folders in the directory, and the second command will print the output All the files and folders are listed.
#!/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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With