6

I would like to create a subprocess of a process.

What would be a working example which shows how to accomplish this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sasa
  • 61
  • 1
  • 1
  • 2

6 Answers6

6

Start with the subprocess documentation.

If you want to get the output:

>>> import subprocess
>>> output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]
>>> output
'Linux'

If you just want to call and not deal with the output:

>>> subprocess.call(['echo', 'Hi'])
Hi
0

subprocess.check_call is the same except that it throws up a CalledProcessError in case the command is called with invalid parameters.

A good subprocess tutorial.

user225312
  • 126,773
  • 69
  • 172
  • 181
  • yes that what i want. one more question, when i will use "output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0] " this command, this runs perfectly fine... but if i have to run like "rdiff-backup -v --force <> <> <> <>" then if i split them using spaces and apply that list to this command gives me error...so my question is do i have to write my command in list by splitting with spaces??? – sasa Dec 20 '10 at 12:30
  • You should have a list preferably as arguments to `Popen`. If you have a string, do a string.split() and then pass it to `Popen`. – user225312 Dec 20 '10 at 12:31
  • thanks sukhbir, I have done that splitting of list but not working with this...also this output is the output which comes when complete execution occurs...how to retrieve the continuous output, rather than taking output after complete executaion? – sasa Dec 20 '10 at 12:40
  • The whole purpose of the `communicate` method is to wait for the process to finish and return all the output. If you don't want to wait, don't call `communicate`. Instead, read from the stdout or stderr attribute to read the output. – user225312 Dec 20 '10 at 12:44
  • sorry i am new in this python so asking loads of questions, but we are reading from stdout itself in our command - " subprocess >>> output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0] "...then can you please elaborate? – sasa Dec 20 '10 at 12:51
  • We are reading from the terminal and not the standard input/ output. Notice: `stdout=subprocess.PIPE`. – user225312 Dec 20 '10 at 12:56
  • oh ok. then can you please tell me how to read using subprocesses for cont. output? – sasa Dec 20 '10 at 13:03
  • http://stackoverflow.com/questions/874815/how-do-i-get-real-time-information-back-from-a-subprocess-popen-in-python-2-5 – user225312 Dec 20 '10 at 13:06
  • thanks sukhbir, that means i can write as --->>> subprocess >>> output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0] and then, while output.poll() is None: out = output.stdout.readline()...i will try this... – sasa Dec 20 '10 at 13:10
  • You should try it and let me know also cause I have never tried it :) – user225312 Dec 20 '10 at 13:11
3

Launching and monitoring a subprocess:

import subprocess, time, os, signal
args=['/usr/bin/vmstat','-n','2']
app=subprocess.Popen(args=args, stdout=open('somefile','w'))
print "Your app's PID is %s. You can now process data..." % app.pid
time.sleep(5)
if app.poll() == None: print "Process is still running after 5s."
print "The app outputed %s bytes." % len(open('somefile','r').read())
print "Stopping the process..."
os.kill(app.pid, signal.SIGTERM)

There is more to it. Just check the Popen docs.

AXE Labs
  • 4,051
  • 4
  • 29
  • 29
1
import subprocess

subprocess.call(['echo', 'hello world'])
dan_waterworth
  • 6,261
  • 1
  • 30
  • 41
  • what subprocess.call does? i am running a file named test.py and i want to run it creating subprocess. did it means that i have to run it like subprocess.call(['python', 'test.py']) ? – sasa Dec 20 '10 at 09:21
  • take a look at the docs, http://docs.python.org/library/subprocess.html#subprocess.call – dan_waterworth Dec 20 '10 at 09:23
  • thanks dan. only thing which i did not understand is if i am running a file named test.py and i want to run it creating subprocess. did it means that i have to run it like subprocess.call(['python', 'test.py'])? – sasa Dec 20 '10 at 09:26
  • why do you need to create a new interpreter and not just use execfile? subprocess.call is a blocking function, if you need non-blocking operation then use subprocess.Popen instead. – dan_waterworth Dec 20 '10 at 09:30
  • means did subprocess.Popen will create a new process? – sasa Dec 20 '10 at 11:10
  • both Popen and call will create a new process, but call waits until the process has finished before it returns. – dan_waterworth Dec 20 '10 at 11:19
  • @sasa: If your new 'process' is in Python, you could look into `execfile`, or threading, or multiprocessing, depending on what you want to do. `subprocess` is used to tell your computer to run an entirely separate program. – Thomas K Dec 20 '10 at 11:31
  • yes that what i want. one more question, when i will use "output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0] " this command, this runs perfectly fine... but if i have to run like "rdiff-backup -v --force <> <> <> <>" then if i split them using spaces and apply that list to this command gives me error...so my question is do i have to write my command in list by splitting with spaces??? – sasa Dec 20 '10 at 12:27
  • you can using shlex.split to form a list from a string. – dan_waterworth Dec 20 '10 at 15:17
1

This is what worked for me if you want to run a simple command instead of giving a seperate file

import subprocess
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
print process.returncode

To get returncode of process you can use process.returncode To get response you can use process.communicate()

in case if you are confuse you can just test this code by using command="ls"

if you are getting returncode other than 0 then you can check here what that error code means: http://tldp.org/LDP/abs/html/exitcodes.html

For more details about Subprocess: http://docs.python.org/library/subprocess.html

Zohab Ali
  • 8,426
  • 4
  • 55
  • 63
0
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
subprocess.call(os.popen(tempFileName), shell=True)
os.remove(tempFileName)
imp
  • 1,967
  • 2
  • 28
  • 40
0

Based on user225312's answer, I prepared the below one liner, it may help you to test the subprocess:

python -c "import subprocess;
output = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE).communicate()[0]; 
print output"

result like: Linux xxx.xxx.xxx.xxx 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

LingYan Meng
  • 699
  • 4
  • 12