5

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

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.

1
  • subprocess.call was deprecated in 3.5: subprocess.run should be used instead.
    – wordbug
    Jan 22, 2019 at 9:09
8
import os
os.system('./script.sh')

python script won't stop until sh is finished

3
  • "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. Jan 28, 2011 at 9:41
  • 1
    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.
    – Frost.baka
    Jan 28, 2011 at 9:43
  • subprocess.call provides the easy/simple API too ;) Jan 28, 2011 at 14:52
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.

1
  • 3
    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. Jan 28, 2011 at 9:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.