6

I would like to create a subprocess of a process.

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

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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???
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.
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?
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.
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?
|
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.

Comments

1
import subprocess

subprocess.call(['echo', 'hello world'])

9 Comments

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']) ?
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'])?
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.
means did subprocess.Popen will create a new process?
|
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

Comments

0
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
subprocess.call(os.popen(tempFileName), shell=True)
os.remove(tempFileName)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.