5

Will it continue the code after it's run? Or will it stop at that line until the script is done?

asked Jan 28, 2011 at 9:36

3 Answers 3

10

Using subprocess.call is the easiest way. It will not return until the executed program has terminated. Have a look at the other methods of the subprocess module if you need different behaviour.

answered Jan 28, 2011 at 9:37
Sign up to request clarification or add additional context in comments.

1 Comment

subprocess.call was deprecated in 3.5: subprocess.run should be used instead.
8
import os
os.system('./script.sh')

python script won't stop until sh is finished

answered Jan 28, 2011 at 9:38

3 Comments

"The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." - for example using subprocess.call() will not execute a shell to run whatever you run so you won't have to take care of argument injection if you pass any dynamically generated arguments to your program.
You can also suggest subprocess.Popen -- this could allow you to control the execution, wait for it, terminate, etc. But as practice shows os.system often provides all the functionality you need.
subprocess.call provides the easy/simple API too ;)
1

You can use os.system or subprocess.Popen or subprocess.call but when using subprocess methods make sure you use shell=True. And executing it via system call in all these methods is blocking. The python script will complete and then go the next step.

answered Jan 28, 2011 at 9:41

1 Comment

No need to use shell=True.. Shellscripts with a shebang line (or even invoking the shell with the script as an argument) can be executed fine with exec()-style functions.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.