python script to capture output of top command - linux

I was trying to capture output of top command using the following python script:
import os
process = os.popen('top')
preprocessed = process.read()
process.close()
output = 'show_top.txt'
fout = open(output,'w')
fout.write(preprocessed)
fout.close()
However, the script does not work for top. It gets stuck for a long time. However it works well with commands like 'ls'. I have no clue why this is happening?

Since you're waiting for the process to finish, you need to tell top to only print its output once, and then quit.
You can do that by running:
top -n 1

-b argument required when stdout read from python
os.popen('top -b -n 1')
top -b -n 1

Related

Write output of pdftohtml to stdout

I'd like to run pdftohtml for a pdf file and write its output to /dev/stdout or something that permits me to catch output direct from subprocess.
My code:
cmd = ['pdftohtml', '-c', '-s', '-i', '-fontfullname', filename, '-stdout', '/dev/stdout']
result = subprocess.run(cmd, stdout=PIPE, stderr=STDOUT, text=True)
The code above exits with code -11.
I'm running it with Ubuntu 18.04 inside WSL 2.
I've tried to execute the same command in bash:
[1] 14041 segmentation fault (core dumped) pdftohtml -c -s -i -fontfullname -stdout /dev/stdout
It's also not possible to pass "-" to stdout value.
What can I do to get html output direct from subprocess.run?
I know it's possible to pipe cat and output filename to command, but it's not what I looking for.
The solution must be compatible with WSL2 and python stretch docker image. However, any clarification would be helpful : )
"Complex output mode", -c, specifies output using frames. This only works when writing to files.
If you want to write to stdout, stick to only -s without -c -- and leave out /dev/stdout as an argument ("stdout" is a pre-opened file descriptor; because it's already opened, there's no reason to use a name to open it, so -stdout is a flag-type option, rather than an option that takes an option-argument).

Equivalent of bash "|" in python3 [duplicate]

I want to use subprocess.check_output() with ps -A | grep 'process_name'.
I tried various solutions but so far nothing worked. Can someone guide me how to do it?
To use a pipe with the subprocess module, you have to pass shell=True.
However, this isn't really advisable for various reasons, not least of which is security. Instead, create the ps and grep processes separately, and pipe the output from one into the other, like so:
ps = subprocess.Popen(('ps', '-A'), stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', 'process_name'), stdin=ps.stdout)
ps.wait()
In your particular case, however, the simple solution is to call subprocess.check_output(('ps', '-A')) and then str.find on the output.
Or you can always use the communicate method on the subprocess objects.
cmd = "ps -A|grep 'process_name'"
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = ps.communicate()[0]
print(output)
The communicate method returns a tuple of the standard output and the standard error.
Using input from subprocess.run you can pass the output of one command into a second one.
import subprocess
ps = subprocess.run(['ps', '-A'], check=True, capture_output=True)
processNames = subprocess.run(['grep', 'process_name'],
input=ps.stdout, capture_output=True)
print(processNames.stdout.decode('utf-8').strip())
See the documentation on setting up a pipeline using subprocess: http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline
I haven't tested the following code example but it should be roughly what you want:
query = "process_name"
ps_process = Popen(["ps", "-A"], stdout=PIPE)
grep_process = Popen(["grep", query], stdin=ps_process.stdout, stdout=PIPE)
ps_process.stdout.close() # Allow ps_process to receive a SIGPIPE if grep_process exits.
output = grep_process.communicate()[0]
Also, try to use 'pgrep' command instead of 'ps -A | grep 'process_name'
You can try the pipe functionality in sh.py:
import sh
print sh.grep(sh.ps("-ax"), "process_name")
command = "ps -A | grep 'process_name'"
output = subprocess.check_output(["bash", "-c", command])

isatty() always returning False?

I want to pipe data via stdin to a Python script for onwards processing. The command is:
tail -f /home/pi/ALL.TXT | python3 ./logcheck.py
And the Python code is:
import sys
while 1:
if (sys.stdin.isatty()):
for line in sys.stdin:
print(line)
I want the code to continuously watch stdin and then process each row when received. The tail command is working when run on its own but the python script never outputs anything.
Checking isatty() it appears to always return False?
Help!
A TTY is when you use your regular terminal - as in opening up a python in your shell, and typing
BASH>python
>>>from sys import stdin
>>>stdin.isatty() #True
In your case the standard input is coming from something which is not a tty. Just add a not in the if statement.

Linux - Redirection of a shell script into a text file

I'm new to Linux, and have been trying to solve an assignment but to no avail.
I have a shell script which prints out lines of a text file in a certain manner (a line within every few seconds):
python << END
import time,random
a= open ('/home/ch/pshety/course/fielding_history.txt','r')
flag =False
for i in range(1000):
b=a.readline()
if i==402 or flag:
print(a.readline())
flag=True
time.sleep(2)
END
sh th.sh
If I run it without trying to redirect it anywhere, I get the output on the terminal. However, when I tried to redirect it into a new text file, it doesn't do anything - the text remains empty:
sh th.sh > debug.txt
I've tried looking for answers, I've stumbled upon a lot of suggestions including tee but nothing helps - the file remains empty.
What am I doing wrong?
Try this:
import time,random
a = open('/home/ch/pshety/course/fielding_history.txt', 'r')
for i in range(1000):
b = a.readline()
if i >= 402:
print(b, flush=True)
time.sleep(2)
Your Python script likely needs to flush the contents of the output buffer before you can see it.
Note: aside from the sleep() call, Unix provides other ways of accomplishing this. I would take a look at man tail and read about the -f and -n switches.
Edit: didn't realize that tail has a switch (-s) to sleep as well!

How to execute an external execution file with parameters in Python 3?

I want to execute an exe file using Python 3.4.
That is,
C:/crf_test.exe -m input.txt output.txt
When I executed this at the command line, the result was:
Go SEARCH
to O
...
But, when I executed this in Python like this:
import os
os.startfile('crf_test.exe -m model.txt test.txt')
Nothing happened (I mean appeared in the result window.)
Using os.popen() you can execute and read commands:
cmd = os.popen(r'crf_test.exe -m model.txt test.txt')
result = cmd.read()

Resources