The program being executed is called a process. A process can be an application running on the current operating system or an application related to the operating system. If an application is tied to the operating system, it first creates a process to execute itself.
Other applications rely on operating system services for execution. Most applications are operating system services and background applications that maintain the operating system, software, and hardware.
In python we have different ways to check if the application is open or not. Let’s learn about them in detail one by one.
psutil is a module in python that provides users with an interface to retrieve information about running processes and system utilization. It can be used on mainstream operating systems and API platforms such as Linux, windows, macOs, solaris, and AIX.
The process_iter() function of the psutil module helps us retrieve information about the running process, such as process name, process ID, CPU usage, memory usage, etc. It also provides information about system utilization such as disk usage, network usage, etc.
In this example, we are trying to find out if a process named "Chrome.exe" is currently running on 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")
False
This is another example of the process_iter() function of the psutil module, which provides details of a 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}%")
The following is the output of process_iter(), which provides full 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%
The subprocess module is another way to check if an application is running or stopped. Using the subprocess module, we can start a new application from the current Python program. We can use the check_output() method to obtain the output of programs and commands.
In the example below, we try to use the check_output() function to verify that the application is open –
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")
True
Windows Management Instrumentation is a set of tools in the Windows operating system that allows administrators to manage remote and local computers.
In Python, we have the wmi module, which helps us check whether the application is running. The following code is used to install wmi in a python environment.
pip install wmi
In this example, we pass the application name as an input parameter to the WMI() function of the wmi module to retrieve the status of the application with the process ID.
import wmi f = wmi.WMI() for process in f.Win32_Process(): print(f"{process.ProcessId:>5} {process.Name}")
The following 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
The above is the detailed content of How to check if application is open in Python?. For more information, please follow other related articles on the PHP Chinese website!