Running a linux command from python
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.
How to execute linux commands using python
The syntax for python is:
import os
os.system("command")
For bash .sh like Mac os
How do I execute a program or call a system command?
Use the subprocess
module in the standard library:
import subprocess
subprocess.run(["ls", "-l"])
The advantage of subprocess.run
over os.system
is that it is more flexible (you can get the stdout
, stderr
, the "real" status code, better error handling, etc...).
Even the documentation for os.system
recommends using subprocess
instead:
The
subprocess
module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in thesubprocess
documentation for some helpful recipes.
On Python 3.4 and earlier, use subprocess.call
instead of .run
:
subprocess.call(["ls", "-l"])
Run linux command from python which needs user input
In his comment, Jean-François Fabre is right about this:
But the real issue is that piping input to a password entering routine doesn't always work.
But not about this:
You have to generate a keyboard event at a lower level.
Though it would be true in general, in the case at hand the command openssl genrsa …
offers the option -passout stdin
to read the password from standard input:
openssl genrsa -aes256 -out ise.key.pem -passout stdin 2048
So, this Python script works:
import subprocess
p=subprocess.Popen(["openssl","genrsa","-aes256","-out","ise.key.pem",
"-passout","stdin","2048"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
inputdata="123456"
p.communicate(input=inputdata)
Note that one cannot call communicate()
with timeout=None more than once, since it waits for process to terminate. But it isn't necessary either, because with -passout stdin
the command doesn't ask for verifying, thus no need to enter the pass phrase a second time.
How to run a command as root in python?
This package requires root permissions and root access. So, it must run as root and the package must be installed as root.
sudo commands are useful for this.
sudo python selenium_script.py
will run it as root. If this alone does not solve, you must install it as root too. Navigate to your terminal and cd to working directory (or home directory). Then, run this command:
sudo pip3 install keyboard # use pip if on python 2
Related Topics
Parse HTML Table to Python List
How to Install and Import Python Modules at Runtime
Why Are Scripting Languages (E.G. Perl, Python, and Ruby) Not Suitable as Shell Languages
Parsing Date/Time String with Timezone Abbreviated Name in Python
Automatically Create Requirements.Txt
How to Split Elements of a List
How to Save a Python Interactive Session
Lag When Win.Blit() Background Pygame
How to Make Sure If Some HTML Elements Are Loaded for Selenium + Python
How to Build 32Bit Python 2.6 on 64Bit Linux
Which Key/Value Store Is the Most Promising/Stable
Matplotlib: Format Axis Offset-Values to Whole Numbers or Specific Number
What's the How to Install Pip, Virtualenv, and Distribute for Python