Note that there are some explanatory texts on larger screens.

plurals
  1. POredirecting shell output using subprocess
    primarykey
    data
    text
    <p>I have a python script which calls a lot of shell functions. The script can be run interactively from a terminal, in which case I'd like to display output right away, or called by crontab, in which case I'd like to email error output.</p> <p>I wrote a helper function for calling shell functions:</p> <pre><code>import subprocess import shlex import sys def shell(cmdline, interactive=True): args = shlex.split(cmdline.encode("ascii")) proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) val = proc.communicate() if interactive is True: if proc.returncode: print "returncode " + str(proc.returncode) print val[1] sys.exit(1) else: print val[0] else: if proc.returncode: print "" # send email with val[0] + val[1] if __name__ == "__main__": # example of command that produces non-zero returncode shell("ls -z") </code></pre> <p>The problem I'm having is two-fold.</p> <p>1) In interactive mode, when the shell command takes a while to finish (e.g. few minutes), I don't see anything until the command is completely done since communicate() buffers output. Is there a way to display output as it comes in, and avoid buffering? I also need a way to check the returncode, which is why I'm using communicate().</p> <p>2) Some shell commands I call can produce a lot of output (e.g. 2MB). The <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow">documentation</a> for communicate() says "do not use this method if the data size is large or unlimited." Does anyone know how large is "large"?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload