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.

1
  • 1
    @eugeney: Then it's probably better to use one of the convenience functions, eg subprocess.check_output, rather than directly calling subprocess.Popen.
    – PM 2Ring
    Commented Jan 29, 2016 at 14:45
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
  • 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. Commented Jan 29, 2016 at 14:00
  • 1
    @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. Commented Jan 29, 2016 at 14:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.