{"id":6684,"date":"2021-08-16T09:06:44","date_gmt":"2021-08-16T03:36:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=6684"},"modified":"2021-11-22T18:39:34","modified_gmt":"2021-11-22T13:09:34","slug":"python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-check-if-a-process-is-running-by-name-and-find-its-process-id-pid\/","title":{"rendered":"Python: Check if a Process is Running by Name and find its Process ID (PID) | How to find the PID of a Running Process?"},"content":{"rendered":"
Do you want to determine whether a process is running or not by name and find its Process ID as well? Then, this is the right place as this article will shed light on you completely on how to check if a process is running or not using Name. We have provided enough examples for finding the Process ID PID by Name for better understanding. You can visit our site online to seek guidance or simply copy-paste the code from our portal and try to apply this knowledge and write a code on your own.<\/p>\n
In this article, we will discuss finding a running process PIDs by name using Follow the simple guidelines for checking if a process is running or not. They are as such<\/p>\n To check if a process is running or not we will iterate over all the running processes using So let’s see this example to check chrome process is running or not.<\/p>\n <\/p>\n Output:<\/strong><\/p>\n As in my system chrome instances are not running it will return False.<\/p>\n In the above program, we checked any chrome instances were running or not. But in this example, we will see how to get the Process ID of a running \u2018chrome.exe\u2019 process. For that we will iterate over all the running processes and during iteration for each process whose name contains the given string, we will keep its information in a list. The following code helps you find the PID of a running process by Name.<\/p>\n <\/p>\n Now, the contents of the list<\/p>\n This can also be done using a single line using list comprehension\u00a0 i.e<\/p>\n And the same output will come like<\/p>\n Related Programs:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Do you want to determine whether a process is running or not by name and find its Process ID as well? Then, this is the right place as this article will shed light on you completely on how to check if a process is running or not using Name. We have provided enough examples for …<\/p>\npsutil<\/code>.<\/p>\n
psutil<\/code> library can be installed by using<\/p>\n
pip install psutil<\/pre>\n
<\/a>Determine if Process is Running or Not<\/h3>\n
\n
psutil.process_iter()<\/code> and during iteration match the process name.<\/p>\n
import psutil\r\ndef checkProcessRunning(processName):\r\n # Checking if there is any running process that contains the given name processName.\r\n #Iterate over the all the running process\r\n for proc in psutil.process_iter():\r\n try:\r\n # Check if process name contains the given name string.\r\n if processName.lower() in proc.name().lower():\r\n return True\r\n except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):\r\n pass\r\n return False;\r\n\r\n# Checking if any chrome process is running or not.\r\nif checkProcessRunning('chrome'):\r\n print('Yes chrome process was running')\r\nelse:\r\n print('No chrome process was running')<\/pre>\n
No chrome process was running<\/pre>\n
<\/a>Finding the Process ID(PID) by Name?<\/h3>\n
import psutil\r\ndef findProcessIdByName(processName):\r\n # Here is the list of all the PIDs of a all the running process \r\n # whose name contains the given string processName\r\n listOfProcessObjects = []\r\n #Iterating over the all the running process\r\n for proc in psutil.process_iter():\r\n try:\r\n pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])\r\n # Checking if process name contains the given name string.\r\n if processName.lower() in pinfo['name'].lower() :\r\n listOfProcessObjects.append(pinfo)\r\n except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :\r\n pass\r\n return listOfProcessObjects;\r\n\r\n# Finding PIDs od all the running instances of process \r\n# which contains 'chrome' in it's name\r\nlistOfProcessIds = findProcessIdByName('chrome')\r\nif len(listOfProcessIds) > 0:\r\n print('Process Exists | PID and other details are')\r\n for elem in listOfProcessIds:\r\n processID = elem['pid']\r\n processName = elem['name']\r\n processCreationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(elem['create_time']))\r\n print((processID ,processName,processCreationTime ))\r\nelse :\r\n print('No Running Process found with this text')\r\n<\/pre>\n
(<\/span>2604<\/span>, <\/span>'chrome.exe'<\/span>, <\/span>'2018-11-10 19:12:13'<\/span>)\r\n<\/span>(4276<\/span>, <\/span>'chrome.exe'<\/span>, <\/span>'2018-11-10 19:12:14'<\/span>)\r\n<\/span>(9136<\/span>, <\/span>'chrome.exe'<\/span>, <\/span>'2018-11-10 19:12:14'<\/span>)\r\n<\/span>(9616<\/span>, <\/span>'chrome.exe'<\/span>, <\/span>'2018-11-10 19:43:41'<\/span>)\r\n<\/span>(12904<\/span>, <\/span>'chrome.exe'<\/span>, <\/span>'2018-11-10 19:12:13'<\/span>)\r\n<\/span>(13476<\/span>, <\/span>'chrome.exe'<\/span>, <\/span>'2018-11-10 20:03:04'<\/span>)\r\n<\/span>(15520<\/span>, <\/span>'chrome.exe'<\/span>, <\/span>'2018-11-10 20:02:22'<\/span>)<\/span><\/pre>\n<\/div>\n
# Finding the PIDs of all the running instances of process that contains 'chrome' in it's name\r\n<\/span>procObjList = [<\/span>procObj <\/span>for<\/span> procObj <\/span>in<\/span> psutil.<\/span>process_iter<\/span>()<\/span> if<\/span> 'chrome'<\/span> in<\/span> procObj.<\/span>name<\/span>()<\/span>.<\/span>lower<\/span>()<\/span> ]<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n
psutil.<\/span>Process<\/span>(<\/span>pid=<\/span>2604<\/span>, name=<\/span>'chrome.exe'<\/span>, started=<\/span>'19:12:13'<\/span>)\r\n<\/span>psutil.Process<\/span>(<\/span>pid=<\/span>4276<\/span>, name=<\/span>'chrome.exe'<\/span>, started=<\/span>'19:12:14'<\/span>)\r\n<\/span>psutil.Process<\/span>(<\/span>pid=<\/span>9136<\/span>, name=<\/span>'chrome.exe'<\/span>, started=<\/span>'19:12:14'<\/span>)\r\n<\/span>psutil.Process<\/span>(<\/span>pid=<\/span>9616<\/span>, name=<\/span>'chrome.exe'<\/span>, started=<\/span>'19:43:41'<\/span>)\r\n<\/span>psutil.Process<\/span>(<\/span>pid=<\/span>12904<\/span>, name=<\/span>'chrome.exe'<\/span>, started=<\/span>'19:12:13'<\/span>)\r\n<\/span>psutil.Process<\/span>(<\/span>pid=<\/span>13476<\/span>, name=<\/span>'chrome.exe'<\/span>, started=<\/span>'20:03:04'<\/span>)\r\n<\/span>psutil.Process<\/span>(<\/span>pid=<\/span>15520<\/span>, name=<\/span>'chrome.exe'<\/span>, started=<\/span>'20:02:22'<\/span>)<\/span><\/pre>\n<\/div>\n
\n