Live output status from subprocess command Python [duplicate] - python

This question already has answers here:
live output from subprocess command
(21 answers)
Closed 2 years ago.
I'm writing a script to get netstat status using subprocess.check_output.
cmd = 'netstat -nlpt'
result = subprocess.check_output(cmd, shell=True, timeout=1800)
print(result.decode('utf-8'))
The above is running perfectly. Is there any way to get the live-streaming output. I have heard poll() function does this job. In live output from subprocess command they are using popen but i'm using check_output please some one help me on this issue. Thank you!

From this answer:
The difference between check_output and Popen is that, while popen is a non-blocking function (meaning you can continue the execution of the program without waiting the call to finish), check_output is blocking.
Meaning if you are using subprocess.check_output(), you cannot have a live output.
Try switching to Popen().

Related

How to call combined linux commands on python subprocess [duplicate]

This question already has answers here:
How to use `subprocess` command with pipes
(7 answers)
Closed 5 years ago.
I write this command on linux
netstat -ant | wc -l
but when I try to call from python with
subprocess.Popen(['netstat','-ant','|','wc','-l'])
I cant get all output, I see just result of first command (netstat -ant).
How can I process this command on python ? (note: this command gives a int as a result)
I don't know if there's any easier method but you can go like:
from subprocess import run, Popen, PIPE
sess1 = run(['netstat', 'ant'], stdout=PIPE)
sess2 = Popen(['grep', '"SYN"'], stdin=PIPE)
sess2.stdin.write(sess1.stdout)
sess2.communicate() # required?

Python - read output from long-running subprocess [duplicate]

This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed 6 years ago.
Using the subprocess module (Python 2.7), I'm running a command and attempting to process its output as it runs.
I have code like the following:
process = subprocess.Popen(
['udevadm', 'monitor', '--subsystem=usb', '--property'],
stdout=subprocess.PIPE)
for line in iter(process.stdout.readline, ''):
print(line)
However, the output only gets printed after I Ctrl+C, even if I add sys.stdout.flush() after the print statement.
Why is this happening, and how can I live stream the output from this process?
Notably, this udevadm monitor command is not intended to terminate, so I can't simply wait for the process to terminate and process its output all at once.
I found live output from subprocess command but the approach in the accepted answer did not solve my problem.
You could use unbuffer :
process = subprocess.Popen(
["unbuffer", 'udevadm', 'monitor', '--subsystem=usb', '--property'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
print(line)

subprocess.Popen not printing/running properly [duplicate]

This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed 7 years ago.
I have a script that reads from external sensors (and runs forever), when I run it as ./zwmeter /dev/ttyUSB0 300 it behaves normally and prints output continuously to stdout. I am using bash on Ubuntu. I'd like to execute this command as part of a python script. I have tried:
from subprocess import Popen, PIPE
proc = Popen(['./zwmeter', '/dev/ttyUSB0', '300'], stderr=PIPE, stdout=PIPE)
print proc.communicate()
but I get a program that runs forever without producing any output. I don't care about stderr, only stdout and have tried splitting up the printing but still with no success.
Thank you for any help you can provide!
I think the problem has to do with the process I'm calling not terminating. I found a good work around on this site:
http://eyalarubas.com/python-subproc-nonblock.html

Run programmes in a sequence using python [duplicate]

This question already has answers here:
Why is subprocess.Popen not waiting until the child process terminates?
(3 answers)
Closed 8 years ago.
I have a python programme as below
import os
import subprocess
for m in range(0,10):
os.chdir("C:/")
run="my command%d"%m
subprocess.Popen(run).wait()
Where 'my command' is something I used to launch another programme.
Although I have wait() after Popen, it turns out that the 10 programmes still run simultaneously, not as expected.
How do I settle this issue?
Two options:
use subprocess.check_call() (which should run sequentially)
use Popen.communicate() ( https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate) with stdout and stderr set to subprocess.PIPE to see if stdout and stderr are indeed sequentially generated
Also, datetime.datetime.now() gives you a microsecond, so you can see time with granularity higher than 1s.

Python subprocess get child's output [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
How to get output from subprocess.Popen()
Retrieving the output of subprocess.call()
Here is my question. I have an executable called device_console. The device_console provides a command line interface to a device. In device_console, four commands can be run: status, list, clear and exit. Each command produces an output. As an example:
[asdfgf#localhost ~]$ device_console
device_console> list
File A
File B
device_console> status
Status: OK
device_console> clear
All files cleared
device_console> list
device_console> exit
[asdfgf#localhost ~]$
As part of testing, I want to get the output of each command. I want to use Python for it. I am looking at Python's subprocess, but somehow I am unable to put them together. Can you please help?
Use the subprocess module. Example:
import subprocess
# Open the subprocess
proc = subprocess.open('device_console', stdin=subprocess.PIPE, stdout.subprocess.PIPE)
# Write a command
proc.stdin.write('list\n')
# Read the results back -- this will block until a line of input is received
listing = proc.stdout.readline()
# When you're done, close the input stream so the subprocess knows to exit
proc.stdin.close()
# Wait for subprocess to exit (optional) and get its exit status
exit_status = proc.wait()
it sounds like you want something more like 'Expect'.
check out Pexpect
"Pexpect is a pure Python module that
makes Python a better tool for
controlling and automating other
programs. Pexpect is similar to the
Don Libes Expect system, but Pexpect
as a different interface that is
easier to understand. Pexpect is
basically a pattern matching system.
It runs programs and watches output.
When output matches a given pattern
Pexpect can respond as if a human were
typing responses."

Categories