Run Process and Don’t Wait

This call doesn’t wait for the child process to terminate (on Linux). Don’t ask me what close_fds does; I wrote the code some years ago. (BTW: The documentation of subprocess.Popen is confusing, IMHO.) proc = Popen([cmd_str], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True) Edit: I looked at the the documentation of subprocess, and I believe the important … Read more

Python read from subprocess stdout and stderr separately while preserving order

The code in your question may deadlock if the child process produces enough output on stderr (~100KB on my Linux machine). There is a communicate() method that allows to read from both stdout and stderr separately: from subprocess import Popen, PIPE process = Popen(command, stdout=PIPE, stderr=PIPE) output, err = process.communicate() If you need to read … Read more

How can I specify working directory for a subprocess

subprocess.Popen takes a cwd argument to set the Current Working Directory; you’ll also want to escape your backslashes (‘d:\\test\\local’), or use r’d:\test\local’ so that the backslashes aren’t interpreted as escape sequences by Python. The way you have it written, the \t part will be translated to a tab. So, your new line should look like: … Read more

How to run a subprocess with Python, wait for it to exit and get the full stdout as a string?

I am using the following construct, although you might want to avoid shell=True. This gives you the output and error message for any command, and the error code as well: process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait for the process to terminate out, err = process.communicate() errcode = process.returncode

Python: How to prevent subprocesses from receiving CTRL-C / Control-C / SIGINT

Somebody in the #python IRC-Channel (Freenode) helped me by pointing out the preexec_fn parameter of subprocess.Popen(…): If preexec_fn is set to a callable object, this object will be called in the child process just before the child is executed. (Unix only) Thus, the following code solves the problem (UNIX only): import subprocess import signal def … Read more

What’s the difference between escapeshellarg and escapeshellcmd?

Generally, you’ll want to use escapeshellarg, making a single argument to a shell command safe. Here’s why: Suppose you need to get a list of files in a directory. You come up with the following: $path=”path/to/directory”; // From user input $files = shell_exec(‘ls ‘.$path); // Executes `ls path/to/directory` (This is a bad way of doing … Read more

Passing variables to a subprocess call [duplicate]

Try separating the values with commas: subprocess.call([‘useradd’, ‘-m’, ‘-g’, _primarygroup, ‘-G’, _secondarygroup, ‘-u’, _userid, _username]) See http://docs.python.org/library/subprocess.html#subprocess.call – It takes an array where the first argument is the program and all other arguments are passed as arguments to the program. Also don’t forget to check the return value of the function for a zero return … Read more

Python: how to kill child process(es) when parent dies?

Heh, I was just researching this myself yesterday! Assuming you can’t alter the child program: On Linux, prctl(PR_SET_PDEATHSIG, …) is probably the only reliable choice. (If it’s absolutely necessary that the child process be killed, then you might want to set the death signal to SIGKILL instead of SIGTERM; the code you linked to uses … Read more