0

when I execute a command (while true; do date; sleep 1; done) over a Python-Skript nothing is displyed and no logging.

import logging
import sys
import subprocess as SP


logger = logging.getLogger('logging')
logger.setLevel(logging.INFO)

if not logger.handlers:
    log_handler = logging.FileHandler('test_logging.log')
    formatter = logging.Formatter('%(asctime)s %(message)s')
    log_handler.setFormatter(formatter)
    logger.addHandler(log_handler)
    log_handler.setLevel(logging.INFO)

command = 'while true; do date; sleep 1; done'
p = SP.Popen(command, shell=True, stdout=SP.PIPE, stderr=SP.PIPE)
print p.stdout.readlines()

for line in p.stdout.readlines():
    logger.info(line)
    print line

2 Answers 2

1

This works for me (on Python 2.6.6)

import subprocess as SP
command = 'while true; do date; sleep 1; done'
p = SP.Popen(command, shell=True, bufsize=1, stdout=SP.PIPE, stderr=SP.PIPE)
while True:
    print p.stdout.readline()

The bufsize=1 isn't essential, but it turns on line buffering, which should improve the efficiency a little.

Sign up to request clarification or add additional context in comments.

1 Comment

@eugeney: Then it's probably better to use one of the convenience functions, eg subprocess.check_output, rather than directly calling subprocess.Popen.
1

p.stdout.readlines() tries to read all the lines of the command's output in a list, which will never finish since the command is an infinite loop: 'while true; do date; sleep 1; done'.

If you're using Python 2, one way of iterating over the lines of the output could be:

for line in iter(p.stdout.readline, b''):
    print line.rstrip()

In Python 3 you can simply iterate over the p.stdout file object.

2 Comments

This just hangs if using Python 2 (as implied by print syntax) on an Ubuntu 14.04 box. It works if I use Python 3. I don't know enough about the internals of subprocess.PIPE to speculate on why.
@J Richard: Indeed, iterating over the file object doesn't seem to work in Python 2. I've updated the post with a working version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.