{"id":58063,"date":"2024-01-18T19:34:01","date_gmt":"2024-01-18T19:34:01","guid":{"rendered":"https:\/\/www.askpython.com\/?p=58063"},"modified":"2025-04-10T20:46:31","modified_gmt":"2025-04-10T20:46:31","slug":"check-linux-process-status-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/check-linux-process-status-python","title":{"rendered":"How to check if a process is still running using Python on Linux?"},"content":{"rendered":"\n<p>In Linux, the process is executing an instance of a running program. It represents a particular running program or command. When you execute a program, the operating system creates a process to run that program. Each process has its unique process ID (PID). You can use commands like &#8216;ps&#8217;, and &#8216;top&#8217; to see running process details.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Python can be used to check if a process is running using libraries like &#8216;psutil&#8217; and modules like &#8216;subprocess&#8217;. The &#8216;psutil&#8217; library allows iterating over running processes to find a specific one, while the &#8216;subprocess&#8217; module can leverage system commands like &#8216;pgrep&#8217; for process monitoring. Both methods are essential for efficient system management, preventing duplicate processes, and ensuring dependencies are met<\/em>.<\/p>\n<\/blockquote>\n\n\n\n<p>For example, use the &#8216;top&#8217; command to get details of running processes.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"521\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/top_cmd-1024x521.png\" alt=\"Top Cmd\" class=\"wp-image-58085\" style=\"width:721px;height:auto\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/top_cmd-1024x521.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/top_cmd-300x153.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/top_cmd-768x391.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/top_cmd.png 1055w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here by using the &#8216;top&#8217; command, you can see all the details of running processes with their PID.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Approach to Process Monitoring<\/h2>\n\n\n\n<p><em> Also read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/python3-alias-as-python\" data-type=\"post\" data-id=\"57750\">How to Create Python3 Aliases in Windows and Ubuntu\/Linux<\/a><\/em><\/p>\n\n\n\n<p>We can check whether a process is running using some Python libraries. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Monitoring Processes with Python&#8217;s psutil Library<\/h3>\n\n\n\n<p>To use the Python <strong>&#8216;psutil&#8217;<\/strong> library, you have to install the <strong>&#8216;psutil&#8217;<\/strong> library using the following command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npip install psutil\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"85\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil-1024x85.png\" alt=\"Psutil\" class=\"wp-image-58092\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil-1024x85.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil-300x25.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil-768x64.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil-1536x128.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil.png 1720w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The &#8216;<strong>psutil<\/strong>&#8216; is successfully installed.<\/p>\n\n\n\n<p>Now to check that the process is running, first create a Python file using the <strong>vi<\/strong> text editor by using the following command.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nvi check_process.py\n<\/pre><\/div>\n\n\n<p>Here we create a Python file named &#8216;<strong>check_process.py<\/strong>&#8216;. Press &#8216;i&#8217; for insert mode and now start typing. The Python script to check if the process is running is given below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport psutil\n\ndef process_status(process_name):\n    for process in psutil.process_iter(&#x5B;&#039;pid&#039;, &#039;name&#039;]):\n        if process.info&#x5B;&#039;name&#039;] == process_name:\n            return True\n    return False\n\n# Example\nprocess_name = &quot;sshd&quot;\nif process_status(process_name):\n    print(f&quot;The process {process_name} is running.&quot;)\nelse:\n    print(f&quot;The process {process_name} is not running.&quot;)\n<\/pre><\/div>\n\n\n<p>Now press <strong>Esc<\/strong> type <strong>&#8216;:wq!&#8217;<\/strong> and press enter to save and exit. This writes the changes and quits the editor. Here we use process <strong>&#8216;sshd&#8217;<\/strong>, you can replace the process_name <strong>&#8216;sshd&#8217;<\/strong> with your actual process name.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"377\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_eg1-1.png\" alt=\"Psutil Eg1 1\" class=\"wp-image-58125\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_eg1-1.png 707w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_eg1-1-300x160.png 300w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p>The first line &#8216;<strong>import psutil<\/strong>&#8216;, imports the &#8216;<strong>psutil<\/strong>&#8216; library. After that, we define a function as <strong>process_status<\/strong>. This function takes <strong>process_name<\/strong> as an argument, process_name is the name of the process you want to check for. <\/p>\n\n\n\n<p>The line <strong>&#8216;psutil.process_iter&#8217;<\/strong> is used to iterate over the running processes on the system, retrieving information like PID and process name. The if loop checks if the process is running. <\/p>\n\n\n\n<p>If the name matches with the process name then it returns true which means the process is running and if the loop completes without finding a matching process, the function returns false. <\/p>\n\n\n\n<p>The example part simply uses the defined function <strong>process_status<\/strong> to check process is running or not. We use the process name <strong>&#8216;sshd&#8217;<\/strong>. In the given example, we check for the &#8216;sshd&#8217; process. The code then prints whether the process is running or not.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>Now you can run the Python script using the following command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython check_process.py\n<\/pre><\/div>\n\n\n<p>Here you can replace check_process.py with the actual name of your Python script.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"52\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/sshd_op.png\" alt=\"Sshd Op\" class=\"wp-image-58116\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/sshd_op.png 541w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/sshd_op-300x29.png 300w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/figure>\n\n\n\n<p>The output is <strong>&#8216;The process sshd is running&#8217;<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Let&#8217;s see another example of not running process:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"382\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_eg-3.png\" alt=\"Psutil Eg 3\" class=\"wp-image-58124\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_eg-3.png 745w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_eg-3-300x154.png 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><\/figure>\n\n\n\n<p>This is an example where we use the <strong>hello.py<\/strong> process to check if it running or not. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"551\" height=\"53\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_op-1.png\" alt=\"Psutil Op 1\" class=\"wp-image-58118\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_op-1.png 551w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/psutil_op-1-300x29.png 300w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/figure>\n\n\n\n<p>The hello.py process is not running that&#8217;s why it prints the output as <strong>&#8216;The process hello.py is not running&#8217;<\/strong>. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em><strong>Also read: <a href=\"https:\/\/www.askpython.com\/resources\/popular-python-libraries-online-gaming\" data-type=\"post\" data-id=\"57360\">Most Popular Python Libraries Used In Online Gaming<\/a><\/strong><\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python&#8217;s Subprocess Module for Process Monitoring<\/h3>\n\n\n\n<p>Now let&#8217;s see how to check if the process is running or not using the <strong>&#8216;subprocess&#8217;<\/strong> module. The Python script to check if the process is running is given below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nimport subprocess\n\ndef process_status(process_name):\n    try:\n        subprocess.check_output(&#x5B;&quot;pgrep&quot;, process_name])\n        return True\n    except subprocess.CalledProcessError:\n        return False\n\n# Example\nprocess_name = &quot;systemd&quot;\nif process_status(process_name):\n    print(f&quot;Yes, the process {process_name} is running.&quot;)\nelse:\n    print(f&quot;No, the process {process_name} is not running.&quot;)\n<\/pre><\/div>\n\n\n<p>Here we use process <strong>&#8216;systemd&#8217;<\/strong>, you can replace the process_name <strong>&#8216;systemd&#8217;<\/strong> with the actual name of the process you want to check.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"797\" height=\"406\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/subprocesss_eg-1.png\" alt=\"Subprocesss Eg 1\" class=\"wp-image-58126\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/subprocesss_eg-1.png 797w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/subprocesss_eg-1-300x153.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/subprocesss_eg-1-768x391.png 768w\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" \/><\/figure>\n\n\n\n<p>First, we import the subprocess module using the <strong>&#8216;import subprocess&#8217;<\/strong> line. Then we define a function as &#8216;<strong>process_status&#8217; <\/strong>to check if the process is running. This function takes the <strong>&#8216;process_name&#8217;<\/strong> as an argument representing the process name. <\/p>\n\n\n\n<p>The &#8216;<strong>try<\/strong>&#8216; and &#8216;<strong>except<\/strong>&#8216; block is used for exception handling. The <strong>&#8216;check_output&#8217;<\/strong> function is used to run the <strong>&#8216;pgrep&#8217;<\/strong> command. <strong>&#8216;pgrep&#8217;<\/strong> is a command line that searches for the process by its name and prints the process ID (PID). If the process is found it returns<strong> True<\/strong> and if the process is not found it returns <strong>False<\/strong>. <\/p>\n\n\n\n<p>The example part simply uses the defined function &#8216;<strong>process_status<\/strong>&#8216; to check process is running or not. We use the process name<strong> &#8216;systemd&#8217;<\/strong>. In the given example, we check for the &#8216;<strong>systemd<\/strong>&#8216; process. The script then prints whether the process is running or not.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"52\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/subprocesss_op-1.png\" alt=\"Subprocesss Op 1\" class=\"wp-image-58122\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/subprocesss_op-1.png 548w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/01\/subprocesss_op-1-300x28.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/figure>\n\n\n\n<p>The output is<strong> &#8216;Yes, the process systemd is running&#8217;<\/strong><\/p>\n\n\n\n<p>In this article, we learn how to check if the process is running or not using Python. Here we create a Python script first by using the<strong> &#8216;psutil&#8217; <\/strong>library and then by using the &#8216;<strong>subprocess<\/strong>&#8216; module. <\/p>\n\n\n\n<p>Checking whether the process is running is important for tasks such as checking dependencies, preventing duplicates, etc. So before interacting with the process, you may want to check dependencies. Also, you might want to ensure that only one instance of a particular process is running at a time. How could these techniques be applied in your current projects?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References:<\/h3>\n\n\n\n<p><a href=\"https:\/\/unix.stackexchange.com\/questions\/110698\/how-to-check-which-specific-processes-python-scripts-are-running\" target=\"_blank\" rel=\"noopener\">https:\/\/unix.stackexchange.com\/questions\/110698\/how-to-check-which-specific-processes-python-scripts-are-running<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Linux, the process is executing an instance of a running program. It represents a particular running program or command. When you execute a program, the operating system creates a process to run that program. Each process has its unique process ID (PID). You can use commands like &#8216;ps&#8217;, and &#8216;top&#8217; to see running process [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":64081,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-58063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/58063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=58063"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/58063\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/64081"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=58063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=58063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=58063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}