python run bash script in background

Total
0
Shares
python run bash script in background

Python subprocess library is used to run processes like bash script in the background. It can create new processes, connect with their input/output/error pipes and obtain their error codes.

Code Example –

import subprocess

// run a bash script in background
process_var = subprocess.Popen(["/path/to/your/bashfile.sh"])

// Getting process id
var pid = process_var.pid

// Terminate this process
process_var.terminate()

Introduction

subprocess library of Python is the standard way of starting a process in the background. PEP 324 supports this. It has one class called Popen. You may directly use the run() function like this –

subprocess.run(["ls", "-l"])

Terminating background script after a timeout

You may use communicate() method and pass the timeout value as argument. If the process didn’t complete within timeout then it will be killed. Example –

import subprocess

process_var = subprocess.Popen(["/path/to/your/bashfile.sh"])
try:
    outs, errs = process_var.communicate(timeout=15)
except TimeoutExpired:
    process_var.kill()

Timeout is set in seconds.

How to check if process is still running or terminated?

subprocess library provides poll() function to check if the process is still running or terminated. It returns a returncode whose values are –

  • None – It means the process is running and not terminated.
  • -N – Terminated by some signal N.
  • 0 – Completed
import subprocess

process_var = subprocess.Popen(["/path/to/your/bashfile.sh"])

return_code = process_var.poll()

// return_code = None ==> Process is running
// return_code = -something ==> terminated by something

How to get output from process?

We need to pass stdout=subprocess.PIPE in Popen method to get the output from the process. Check this example –

import subprocess

process_var = subprocess.Popen(["/path/to/your/bashfile.sh"], stdout=subprocess.PIPE)

try:
    outs, errs = process_var.communicate(timeout=15)

    // Get your output here
    print("Output: ", outs)

except TimeoutExpired:
    process_var.kill()

Conclusion

subprocess library is the most recommended way of starting a background process and running bash scripts. We can pass data and read the output easily. It also provides utility methods to keep the track of running process.

Live Demo

Demo might not work in private window (incognito)

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like