Suppressing output in python subprocess call [duplicate]

You can use the stdout= and stderr= parameters to subprocess.call() to direct stdout or stderr to a file descriptor of your choice. So maybe something like this: import os devnull = open(os.devnull, ‘w’) subprocess.call(shlex.split( ‘/usr/local/itms/bin/iTMSTransporter -m lookupMetadata ‘ ‘-apple_id %s -destination %s’ % (self,apple_id, self.destination)), stdout=devnull, stderr=devnull) Using subprocess.PIPE, if you’re not reading from the … Read more

How do I get ‘real-time’ information back from a subprocess.Popen in python (2.5)

Update with code that appears not to work (on windows anyway) class ThreadWorker(threading.Thread): def __init__(self, callable, *args, **kwargs): super(ThreadWorker, self).__init__() self.callable = callable self.args = args self.kwargs = kwargs self.setDaemon(True) def run(self): try: self.callable(*self.args, **self.kwargs) except wx.PyDeadObjectError: pass except Exception, e: print e if __name__ == “__main__”: import os from subprocess import Popen, PIPE def … Read more

Run a program from python, and have it continue to run after the script is killed

The child process receives the same SIGINT as your parent process because it’s in the same process group. You can put the child in its own process group by calling os.setpgrp() in the child process. Popen‘s preexec_fn argument is useful here: subprocess.Popen([‘nohup’, ‘my_command’], stdout=open(‘/dev/null’, ‘w’), stderr=open(‘logfile.log’, ‘a’), preexec_fn=os.setpgrp ) (preexec_fn is for un*x-oids only. There … Read more

subprocess.call using string vs using list

subprocess‘s rules for handling the command argument are actually a bit complex. Generally speaking, to run external commands, you should use shell=False and pass the arguments as a sequence. Use shell=True only if you need to use shell built-in commands or specific shell syntax; using shell=True correctly is platform-specific as detailed below. From the docs: … Read more

How to spawn parallel child processes on a multi-processor system?

What you are looking for is the process pool class in multiprocessing. import multiprocessing import subprocess def work(cmd): return subprocess.call(cmd, shell=False) if __name__ == ‘__main__’: count = multiprocessing.cpu_count() pool = multiprocessing.Pool(processes=count) print pool.map(work, [‘ls’] * count) And here is a calculation example to make it easier to understand. The following will divide 10000 tasks on … Read more

How to attach debugger to a python subproccess?

I’ve been searching for a simple to solution for this problem and came up with this: import sys import pdb class ForkedPdb(pdb.Pdb): “””A Pdb subclass that may be used from a forked multiprocessing child “”” def interaction(self, *args, **kwargs): _stdin = sys.stdin try: sys.stdin = open(‘/dev/stdin’) pdb.Pdb.interaction(self, *args, **kwargs) finally: sys.stdin = _stdin Use it … Read more

“subprocess.Popen” – checking for success and errors

Do you need to do anything with the output of the process? The check_call method might be useful here. See the python docs here: https://docs.python.org/2/library/subprocess.html#subprocess.check_call You can then use this as follows: try: subprocess.check_call(command) except subprocess.CalledProcessError: # There was an error – command exited with non-zero code However, this relies on command returning an exit … Read more

Redirect subprocess stderr to stdout

In Python < v3.5: A close read of the source code gives the answer. In particular, the documentation is misleading when it says: subprocess.STDOUT Special value that (…) indicates that standard error should go into the same handle as standard output. Since stdout is set to “default” (-1, technically) when stderr=subprocess.STDOUT is evaluated, stderr is … Read more

Merging a Python script’s subprocess’ stdout and stderr while keeping them distinguishable

tsk = subprocess.Popen(args,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) subprocess.STDOUT is a special flag that tells subprocess to route all stderr output to stdout, thus combining your two streams. btw, select doesn’t have a poll() in windows. subprocess only uses the file handle number, and doesn’t call your file output object’s write method. to capture the output, do something like: logfile … Read more