Categories: TutorialsUbuntu

How to Start Background Process in Python

Python is a powerful and popular language that provides tons of useful features. It is often used as a scripting language to perform critical process as well as running high traffic websites. One of the best features of python is its ability to easily work with other languages such as C/C++ as well as shell scripting. Sometimes you may need to run background process from your python script. Here python’s ability to interact with Linux shell is very useful. In this article, we will learn how to start background process in Python.

How to Start Background Process in Python

We will be using subprocess python module for this purpose. Open your python script e.g. test.py in a text editor.

Sponsored
class="wp-block-preformatted">$ vi test.py

Add the following code to set execution environment.

#!/usr/bin/env

Next, import subprocess module.

import subprocess

This module features Popen() function that allows you to run shell commands from python. It can be used to run background processes.

Here is how to run a shell command to remove a file using rm command. Please ensure to provide full path to file to avoid errors. You may also need to use sudo command to overcome permission related errors.

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

You need to split the background command into a list of strings and input this list of strings in Popen() function.

You might also want to add ‘&’ character after the command in case you want to run it as background process in shell itself.

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

Save and close the file. Make it an executable with the following command.

$ sudo chmod +x test.py

Run the python script with following command.

$ python test.py

In this article, we have learnt how to run background process in Python. You can use it to run Linux commands, shell scripts and even other python scripts from a given python script.

Also read:

How to Prevent NGINX from Serving .git directory
How to Prevent Apache from Serving .git directory
How to Check if String is Substring of List Items
How to Check if Column is Empty or Null in MySQL
How to Modify MySQL Column to Allow Null

The post How to Start Background Process in Python appeared first on Fedingo.

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

5 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

6 days ago