- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if an application is open in Python?
A program under execution is known as process. A process can be applications running on the current operating system or, applications related to the Operating system. If an application is related to operating system, firstly it will create a process to execute itself.
The other applications rely on operating system services for execution. Most of the applications are OS services and the background applications that maintain the operating system, software and Hardware.
We have different ways in python to check whether if an application is open or not. Let’s see them one by one in detail.
Using the psutil.process_iter() function
psutil is a module in python which provides the users an interface to retrieve the information of the running processes and system utilization. This can be used in major operating systems such as Linux, windows, macOs, solaris and AIX etc., and to the API platforms.
The process_iter() function of the psutil module helps us to retrieve the information regarding the running processes like process name, process ID, CPU usage, memory usage and more. This also provides information about the system utilization like disk usage, network usage etc.
Example
In this example, we are trying to find out whether the process named “Chrome.exe” is currently running in our system.
import psutil def check_if_process_running(process_name): for process in psutil.process_iter(['name']): if process.info['name'] == process_name: return True return False check_if_process_running("Chrome.exe")
Output
False
Example
This is another example of the process_iter() function of the psutil module which gives the details of the process.
import psutil processes = psutil.process_iter() for process in processes: print(f"Process name: {process.name()} | PID: {process.pid}") cpu_percent = psutil.cpu_percent() print(f"CPU usage: {cpu_percent}%") memory_usage = psutil.virtual_memory() print(f"Total memory: {memory_usage.total / 1024 / 1024:.2f} MB") print(f"Available memory: {memory_usage.available / 1024 / 1024:.2f} MB") print(f"Memory usage: {memory_usage.percent}%")
Output
Following is the output of the process_iter() which gives the total information about the application.
Process name: chrome.exe | PID: 15964 Process name: chrome.exe | PID: 16876 CPU usage: 10.6% Total memory: 12152.65 MB Available memory: 5849.83 MB Memory usage: 51.9%
Using subprocess module
The subprocess module is another way to check whether the application is running or halted. Using the subprocess module, we can start a new application from the current Python program. We can get the output of a program, command using the check_output() method.
Example
In the following example we are trying to verify if an application is open using the check_output() function –
import subprocess def is_process_running(process_name): cmd = 'tasklist /fi "imagename eq {}"'.format(process_name) output = subprocess.check_output(cmd, shell=True).decode() if process_name.lower() in output.lower(): return True else: return False is_process_running("chrome.exe")
Output
True
Using wmi module
The Windows Management Instrumentation, is a set of tools in the windows operating system which allows the administrator to manage remote and local computers.
In python we have the wmi module, which helps us to check whether an application is running or not. The following code is used to install wmi in python environment.
pip install wmi
Example
In this example, we will pass the application name as the input argument to WMI() function of the wmi module, to retrieve the status of an application with process id.
import wmi f = wmi.WMI() for process in f.Win32_Process(): print(f"{process.ProcessId:>5} {process.Name}")
Output
The below is the output of the WMI() function of the wmi module.
0 System Idle Process 4 System 124 Registry 524 smss.exe 752 csrss.exe 868 csrss.exe 888 wininit.exe 940 services.exe 960 lsass.exe 320 winlogon.exe 980 svchost.exe 1048 fontdrvhost.exe 1056 fontdrvhost.exe 1144 WUDFHost.exe 1180 svchost.exe 1268 svchost.exe 1292 WUDFHost.exe 1396 svchost.exe 1404 svchost.exe 1412 svchost.exe 1528 svchost.exe 1640 dwm.exe 1660 svchost.exe