I would like to create python3 code to filter system processes by running the linux command 'ps'.
Here is my current code:
import subprocess,tempfile
command='ps -fe | grep java'
f = tempfile.NamedTemporaryFile(delete=False)
run = subprocess.Popen([command],shell=True,stdout=f)
f.name
'/tmp/tmpswfw1_c8'
By running above code, we get a file containing the ps
list, which has a 'java' entry in it. But when I cat
the file, the long lines get cut. E.g. if the real process line is:
tomcat 21294 1 41 Jul06 ? 1-14:29:27 /opt/java/bin/java -Xms4096m -Xmx4096m -jar /opt/microservices/apps-0.0.1-SNAPSHOT.jar --spring.profiles.active=staging
this line is being processed into that tempfile
by python as:
tomcat 21294 1 41 Jul06 ? 1-14:29:27 /opt/java/bin/java -Xms4096m -Xmx4096m -jar /opt/microservices/apps[[other_chars_are_missing_here]]
The width in output file is same with the width in terminal when I run the python code.
How to correctly get the real output of ps
?
Edited by adding:
And I also get same truncated line with subprocess.check_output:
print (subprocess.check_output("ps -fe | grep java", shell=True))