The canonical way to run stuff with Python is to use the subprocess module, but it has a good bunch of functions enigmatically called check_call or check_output and these functions tend to have cryptic warnings like "Do not use stdout=PIPE or stderr=PIPE with this function", so let me provide some more:
Step 1: Run the script
proc = subprocess.Popen(["your_command", "parameter1", "paramter2"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Now the process is running in the background and you have a reference to it.
EDIT: I almost forgot - if you'd like to retrieve the output later, you have to tell Python to create reading pipes for standard output. If you don't do this step, stdout and stderr will just go to your program's standard output and standard error, and communicate won't pick them up in step 2.
Step 2: wait for the process to finish and get its output
stdout, sterr = proc.communicate()
return_code = proc.returncode
communicate allows you to do some more things too:
- pass stdin data to the process (
input= parameter)
- give a time limit for the process to finish, to avoid hanging (
timeout= parameter)
Make sure to also catch and properly handle any exceptions from Popen or communicate.
If you don't care about old Python, there's a simpler method called subprocess.run that Does It All:
completed_process = subprocess.run(
['your_command', 'parameter'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# this starts the process, waits for it to finish, and gives you...
completed_process.returncode
completed_process.stdout
completed_process.stderr
For error-checking you can call completed_process.check_returncode() or just pass check=True as an additional argument to run.