Open In App

Python – Get list of running processes

Improve
Improve
Like Article
Like
Save
Share
Report

A Process is a program that is being executed (processed). A process may not have to be one run explicitly by the user, it could be a system process spawned by the operating system. Any applications that execute on an operating system firstly creates a process of its own in order to execute. In a typical OS installation, most processes are os services and background applications, that are run to maintain the operating system, software and hardware. 
In this article, we will take a look at different ways of obtaining the list of running processes of a Windows OS, through Python. Firstly, we would describe a python method in order to achieve the result and then would look at a command found in the Windows Command Processor for the same. 
Method 1:
We would be using the wmi library for getting the list of running processes on Windows OS. In order the install the module, execute the following command in the command interpreter of your operating system:- 
 

pip install wmi


COde:
 

Python3




import wmi
 
# Initializing the wmi constructor
f = wmi.WMI()
 
# Printing the header for the later columns
print("pid   Process name")
 
# Iterating through all the running processes
for process in f.Win32_Process():
     
    # Displaying the P_ID and P_Name of the process
    print(f"{process.ProcessId:<10} {process.Name}")


Output: 
 

3196       RuntimeBroker.exe
3524       ShellExperienceHost.exe
3548       SearchIndexer.exe
3796       SearchUI.exe
4136       IDMan.exe
4368       IEMonitor.exe
4488       notepad.exe
2616       SettingSyncHost.exe
4212       dasHost.exe
4664       AdaptiveSleepService.exe
4716       svchost.exe
5412       chrome.exe
1376       chrome.exe
1280       cmd.exe
4928       conhost.exe
5596       py.exe
5060       python.exe
1508       WmiPrvSE.exe

Explanation:
Firstly, we initialize the WMI() function of wmi library. This allows us to use the functions found inside it such as WMI.Win32_Service, WMI.Win32_Process, WMI.Win32_Printjob which are designed to perform different tasks. We would be using the WMI.Win32_Process function in order to get the list of running processes on the system. Then we called the function WMI.Win32_Process() to get the running processes, iterated through each process and stored in variable process. Then we obtained the ProcessID (pid) and ProcessName (name) of the process using the associated attributes. We used F-strings for the output in order to add padding to the output to align it properly. 
  
Method 2:
In this method, we would be using a command found inside the Windows Command Processor (cmd.exe) under the name WMIC ( Windows Management Instrumentation Command line) in order to get the desired result. WMIC is a commandline utility that allows users to performs Windows Management Instrumentation (WMI) operations with a command prompt. For the purpose of getting running processes, we would be executing the command:
 

wmic process get description, processid 


Code:
 

Python3




import os
 
# Running the aforementioned command and saving its output
output = os.popen('wmic process get description, processid').read()
 
# Displaying the output
print(output)


Output: 
 

Description               ProcessId

System Idle Process       0
System                    4
smss.exe                  340
csrss.exe                 460
wininit.exe               604
csrss.exe                 624
winlogon.exe              692
services.exe              736
lsass.exe                 756
svchost.exe               844
svchost.exe               904
dwm.exe                   1012
svchost.exe               80
svchost.exe               420
atiesrxx.exe              1076
svchost.exe               1992
svchost.exe               2032
MsMpEng.exe               2052
NisSrv.exe                2852
sihost.exe                3032
taskhostw.exe             2148
GoogleCrashHandler.exe    2712
GoogleCrashHandler64.exe  2704
explorer.exe              2892
RuntimeBroker.exe         3196
ShellExperienceHost.exe   3524
SearchIndexer.exe         3548
chrome.exe                1340
chrome.exe                2216


Note: It is not mandatory to use the os library for the purpose. The user could pick any other alternatives (Subprocess, shutil etc) which allows for commandline command execution.
Explanation: 
We used the function popen() found inside the os module, in order to execute the command in the command processor. Then we passed the output of the above command to read() in order to get data in readable form out of os._wrap_close object. In the end, we displayed the output.

Method 3:

We will use the subprocess module to interact with cmd and to retrieve information into your python ide. we can read the cmd command through the subprocess module.

Let’s see this logic, if we run this wmic process list brief code into our terminal then we got like this:

Approach:

  • import module
  • Get the output for the command “wmic process list brief ” using subprocess.check_output()
  • Now get the Split the string and arrange your data with your own needs.

Code:

Python3




# import module
import subprocess
 
# traverse the software list
Data = subprocess.check_output(['wmic', 'process', 'list', 'brief'])
a = str(Data)
# try block
#  arrange the string
try:
    for i in range(len(a)):
        print(a.split("\\r\\r\\n")[i])
except IndexError as e:
    print("All Done")


Output:

b'HandleCount  Name                                                                Priority  ProcessId  ThreadCount  WorkingSetSize  
0            System Idle Process                                                 0         0          8            8192            
5649         System                                                              8         4          222          1835008         
0            Registry                                                            8         120        4            33910784        
89           smss.exe                                                            11        516        2            532480          
815          csrss.exe                                                           13        652        13           2019328         
217          wininit.exe                                                         13        740        1            3239936         
781          services.exe                                                        9         812        8            8294400         
1843         lsass.exe                                                           9         832        7            14864384        
86           svchost.exe                                                         8         1020       1            1351680         
32           fontdrvhost.exe                                                     8         308        5            196608          
1526         svchost.exe                                                         8         8          12           38608896        
270          WUDFHost.exe                                                        8         592        5            2097152         
1479         svchost.exe                                                         8         1056       11           16363520        
541          svchost.exe                                                         8         1104       15           4509696         
279          svchost.exe                                                         8         1260       3            2584576         
146          svchost.exe                                                         8         1284       1            6389760         
214          svchost.exe                                                         8         1300       3            3452928         
327          svchost.exe                                                         8         1308       6            5795840         
345          svchost.exe                                                         8         1332       13           10571776        
395          svchost.exe                                                         8         1452       7            5079040         
261          svchost.exe                                                         8         1460       5            5914624         
161          svchost.exe                                                         8         1472       2            4902912         
390          svchost.exe                                                         8         1580       11           9826304         
348          WUDFHost.exe                                                        13        1652       12           11526144        
225          svchost.exe                                                         8         1736       2            10473472        
205          svchost.exe                                                         8         1744       1            2093056         
278          svchost.exe                                                         8         1752       2            4444160         
174          svchost.exe                                                         8         1824       4            4063232         
224          svchost.exe                                                         8         1832       6            3821568         
593          svchost.exe                                                         8         2024       5            7610368         
186          svchost.exe                                                         8         1424       2            5058560         
168          igfxCUIService.exe                                                  8         2120       2            3579904         
435          svchost.exe                                                         8         2188       6            12341248        
279          svchost.exe                                                         8         2220       10           5017600         
227          svchost.exe                                                         8         2296       3            7024640         
221          svchost.exe                                                         8         2308       3            1908736         
384          svchost.exe                                                         8         2396       7            8499200         
0            Memory Compression                                                  8         2424       54           298409984       
240          svchost.exe                                                         8         2440       2            4845568         
179          svchost.exe                                                         8         2476       5            3567616         
239          svchost.exe                                                         8         2660       8            5775360         
3352         svchost.exe                                                         8         2684       9            6230016         
225          svchost.exe                                                         8         2816       2            4804608         
487          svchost.exe                                                         8         2872       7            9641984         
473          svchost.exe                                                         8         2912       4            13836288        
142          svchost.exe                                                         8         3032       4            2727936         
633          svchost.exe                                                         8         3048       3            16154624        
555          svchost.exe                                                         8         2072       14           12455936        
267          svchost.exe                                                         8         2936       4            7462912         
465          spoolsv.exe                                                         8         3168       7            4685824         
420          svchost.exe                                                         8         3200       10           8019968         
187          svchost.exe                                                         8         3324       6            2433024         
174          svchost.exe                                                         8         3572       2            2650112         
416          svchost.exe                                                         8         3584       5            13344768        
540          svchost.exe                                                         8         3592       10           24936448        
161          IntelCpHDCPSvc.exe                                                  8         3604       3            2052096         
406          svchost.exe                                                         8         3612       19           25100288  

 



Last Updated : 29 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads