Questions tagged [subprocess]

The Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Use it to run a shell command or an executable in Python.

The Python subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

If you want to run Python code in a separate process consider instead.

is preferable to subprocess in some cases.

Waiting for a command to finish and getting the result

Interacting with a subprocess while it is still running

Windows

Misc

12158 questions
51
votes
2 answers

Redirect subprocess stderr to stdout

I want to redirect the stderr output of a subprocess to stdout. The constant STDOUT should do that, shouldn't it? However, $ python >/dev/null -c 'import subprocess;\ subprocess.call(["ls",…
phihag
  • 278,196
  • 72
  • 453
  • 469
50
votes
10 answers

How to attach debugger to a python subproccess?

I need to debug a child process spawned by multiprocessing.Process(). The pdb degugger seems to be unaware of forking and unable to attach to already running processes. Are there any smarter python debuggers which can be attached to a subprocess?
Maxim Razin
  • 9,114
  • 7
  • 34
  • 33
50
votes
6 answers

"subprocess.Popen" - checking for success and errors

I want to check if a subprocess has finished execution successfully or failed. Currently I have come up with a solution but I am not sure if it is correct and reliable. Is it guaranteed that every process outputs its errors only to stderr…
Zingam
  • 4,498
  • 6
  • 28
  • 48
49
votes
1 answer

subprocess.call using string vs using list

I am trying to use rsync with subprocess.call. Oddly, it works if I pass subprocess.call a string, but it won't work with a list (ala, Python's doc). calling sp.call with a string: In [23]: sp.call("rsync -av content/ writings_raw/",…
oz123
  • 27,559
  • 27
  • 125
  • 187
48
votes
4 answers

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

I have a Python script that I want to use as a controller to another Python script. I have a server with 64 processors, so want to spawn up to 64 child processes of this second Python script. The child script is called: $ python create_graphs.py…
tatlar
  • 3,080
  • 2
  • 29
  • 40
48
votes
5 answers

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

I've tried running things like this: subprocess.Popen(['nohup', 'my_command'], stdout=open('/dev/null', 'w'), stderr=open('logfile.log', 'a')) This works if the parent script exits gracefully, but if I kill the…
James
  • 24,676
  • 13
  • 84
  • 130
48
votes
4 answers

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

The child process is started with subprocess.Popen(arg) Is there a way to ensure it is killed when parent terminates abnormally? I need this to work both on Windows and Linux. I am aware of this solution for Linux. Edit: the requirement of starting…
user443854
  • 7,096
  • 13
  • 48
  • 63
48
votes
2 answers

Python threading multiple bash subprocesses?

How does one use the threading and subprocess modules to spawn parallel bash processes? When I start threads ala the first answer here: How to use threading in Python?, the bash processes run sequentially instead of in parallel.
Andrew
  • 6,295
  • 11
  • 56
  • 95
47
votes
10 answers

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

I'd like to use the subprocess module in the following way: create a new process that potentially takes a long time to execute. capture stdout (or stderr, or potentially both, either together or separately) Process data from the subprocess as it…
Ryan
  • 4,179
  • 6
  • 30
  • 31
46
votes
2 answers

Suppressing output in python subprocess call

For the following command: subprocess.call(shlex.split( """/usr/local/itms/bin/iTMSTransporter -m lookupMetadata -apple_id %s -destination %s"""%(self.apple_id, self.destination)) It prints the entire output into the…
David542
  • 104,438
  • 178
  • 489
  • 842
45
votes
5 answers

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

I am currently working on a wrapper for a dedicated server running in the shell. The wrapper spawns the server process via subprocess and observes and reacts to its output. The dedicated server must be explicitly given a command to shut down…
robert
  • 3,484
  • 3
  • 29
  • 38
45
votes
6 answers

Passing variables to a subprocess call

I am trying to pass my variables from raw_input to my subprocess command. I am new to Python. Any help would he appreciated. #!/usr/bin/python import subprocess print "\nWhat user name" username = str(raw_input('username: ')) print "\nWhat is the…
rsouthard
  • 467
  • 1
  • 4
  • 4
45
votes
5 answers

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

So I noticed subprocess.call while it waits for the command to finish before proceeding with the python script, I have no way of getting the stdout, except with subprocess.Popen. Are there any alternative function calls that would wait until it…
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
44
votes
4 answers

What's the difference between escapeshellarg and escapeshellcmd?

PHP has 2 closely related functions, escapeshellarg() and escapeshellcmd(). They both seem to do similar things, namely help make a string safer to use in system()/exec()/etc. Which one should I use? I just want to be able to take some user input…
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248
44
votes
6 answers

Python: subprocess and running a bash script with multiple arguments

How do I go about running a bash script using the subprocess module, to which I must give several arguments? This is what I'm currently using: subprocess.Popen(['/my/file/path/programname.sh', 'arg1 arg2 %s' % arg3], \ shell = True) The bash…
user2510173
  • 601
  • 1
  • 6
  • 9