How to Run Background Process in Python

Python is a powerful programming language that allows you to create scripts that not only work within Python environment but also interact with other processes such as Linux shell. Many Python-based websites and applications need to run critical processes in background while doing other tasks in the foreground. This is especially required for administrative tasks and system maintenance. Python developers often delegate such tasks to the background so that they do not impact the actual running of website or application. In this article, we will learn how to run background processes in Python.

There are several Python libraries that allow you to easily work with Linux shell. For our purpose, we will use subprocess module.

Open terminal and run the following command to create an empty Python script.

$ vi test.py

Next, add the following line to define execution environment.

#!/usr/bin/env

Next, import subprocess module.

import subprocess

This module offers numerous functions to work with Linux shell as well as command prompt in Windows. We will use Popen() function for our case. It allows you to run shell commands from within Python. It can also be used to run background processes from Python script.

For our example, we will run a shell command to delete a file. Here is the shell command that we typically run in terminal for this purpose.

$ rm -r /path/to/file

To run the above command from Python environment, add the following line to your python script.

subprocess.Popen(["rm","-r","/path/to/file"])

In the above command, we basically split our shell command, using space character, into a list of strings and use this list as input to subprocess.Popen() function. Please make sure to provide full file path avoid 'file not found' error. Also, you may need to add 'sudo' keyword before the command to avoid 'permission denied' error.

In case you want to run a Linux command in the shell's background, then add '&' operator' after the command.

subprocess.Popen(["rm","-r","/path/to/file","&"])

Save and close the file. Make it executable using chmod command.

$ sudo chmod +x test.py

Run your Python script with the following command. It will run your shell command in background.

$ python test.py

That's it. In this article, we have learnt how to run background process in Python. You can use it to run Linux commands, shell scripts and also other Python scripts from a single Python script.