Python is a strong and exponentially growing programming language in the present day. There is a multiple-way to check which script is running in the background of a Linux environment. One of them is using the subprocess module in python. Subprocess is used to run new programs through Python code by creating new processes. In this article, we are going to see how to check any script is running in the background Linux using Python.
Requirement :
Installation of Subprocess :
pip install subprocess.run
We will use subprocess.checkout() methods to get all the running processes.
Syntax : subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=None, timeout=None, text=None, **other_popen_kwargs)
This run command with argument and return output.
stderr=subprocess.STDOUT is used to capture standard error in the result.
Example 1 :
In the below code we will get all the py script running in the background Linux
Python3
import subprocess
pytonProcess = subprocess.check_output("ps -ef | grep .py",shell=True).decode()
pytonProcess = pytonProcess.split('\n')
for process in pytonProcess:
print(process)
Output :
Example 2 :
In the below example we will check whether a particular script is running in background
Python3
import subprocess
pytonProcess = subprocess.check_output("ps -ef | grep test.py",shell=True).decode()
pytonProcess = pytonProcess.split('\n')
for process in pytonProcess:
print(process)
Output :
Example 3 :
In the below code we will get all the PHP script running in background Linux.
Python3
import subprocess
pytonProcess = subprocess.check_output("ps -ef | grep .php",shell=True).decode()
PHPProcess = pytonProcess.split('\n')
for process in PHPProcess:
print(process)
Output :
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
03 Mar, 2021
Like Article
Save Article