<p>The only <em>nice</em> way I've found is:</p>

<pre><code>import sys
import os

try:
        os.kill(int(sys.argv[1]), 0)
        print "Running"
except:
        print "Not running"
</code></pre>

<p>(<a href="http://www.unix.com/unix-advanced-expert-users/79267-trick-bash-scripters-check-if-process-running.html" rel="noreferrer">Source</a>)<br>
But is this reliable? Does it work with every process and every distribution?</p>

[{'answer_id': 38071, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 5, 'selected': False, 'text': '<p>on linux, you can look in the directory /proc/$PID to get information about that process.  In fact, if the directory exists, the process is running.</p>\n'}, {'answer_id': 38196, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 4, 'selected': False, 'text': '<p>It should work on any POSIX system (although looking at the <code>/proc</code> filesystem, as others have suggested, is easier if you know it\'s going to be there).</p>\n\n<p>However: <code>os.kill</code> may also fail if you don\'t have permission to signal the process. You would need to do something like:</p>\n\n<pre><code>import sys\nimport os\nimport errno\n\ntry:\n    os.kill(int(sys.argv[1]), 0)\nexcept OSError, err:\n    if err.errno == errno.ESRCH:\n        print "Not running"\n    elif err.errno == errno.EPERM:\n        print "No permission to signal this process!"\n    else:\n        print "Unknown error"\nelse:\n    print "Running"\n</code></pre>\n'}, {'answer_id': 38230, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 7, 'selected': True, 'text': '<p>Mark\'s answer is the way to go, after all, that\'s why the /proc file system is there. For something a little more copy/pasteable:</p>\n\n<pre><code> &gt;&gt;&gt; import os.path\n &gt;&gt;&gt; os.path.exists("/proc/0")\n False\n &gt;&gt;&gt; os.path.exists("/proc/12")\n True\n</code></pre>\n'}, {'answer_id': 45533, 'author': 'Marius', 'author_id': 4712, 'author_profile': 'https://Stackoverflow.com/users/4712', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>But is this reliable? Does it work with every process and every distribution?</p>\n</blockquote>\n\n<p>Yes, it should work on any Linux distribution. Be aware that /proc is not easily available on Unix based systems, though (FreeBSD, OSX).</p>\n'}, {'answer_id': 4139017, 'author': 'sivabudh', 'author_id': 65313, 'author_profile': 'https://Stackoverflow.com/users/65313', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s the solution that solved it for me:</p>\n\n<pre><code>import os\nimport subprocess\nimport re\n\ndef findThisProcess( process_name ):\n  ps     = subprocess.Popen("ps -eaf | grep "+process_name, shell=True, stdout=subprocess.PIPE)\n  output = ps.stdout.read()\n  ps.stdout.close()\n  ps.wait()\n\n  return output\n\n# This is the function you can use  \ndef isThisRunning( process_name ):\n  output = findThisProcess( process_name )\n\n  if re.search(\'path/of/process\'+process_name, output) is None:\n    return False\n  else:\n    return True\n\n# Example of how to use\nif isThisRunning(\'some_process\') == False:\n  print("Not running")\nelse:\n  print("Running!")\n</code></pre>\n\n<p>I\'m a Python + Linux newbie, so this might not be optimal. It solved my problem, and hopefully will help other people as well.</p>\n'}, {'answer_id': 5112021, 'author': 'timblaktu', 'author_id': 532621, 'author_profile': 'https://Stackoverflow.com/users/532621', 'pm_score': 3, 'selected': False, 'text': "<p>Seems to me a PID-based solution is too vulnerable. If the process you're trying to check the status of has been terminated, its PID can be reused by a new process. So, IMO ShaChris23 the Python + Linux newbie gave the best solution to the problem. Even it only works if the process in question is uniquely identifiable by its command string, or you are sure there would be only one running at a time.</p>\n"}, {'answer_id': 7008599, 'author': 'Maksym Kozlenko', 'author_id': 171847, 'author_profile': 'https://Stackoverflow.com/users/171847', 'pm_score': 0, 'selected': False, 'text': '<p>Sligtly modified version of ShaChris23 script. Checks if proc_name value is found within process args string (for example Python script executed with python ):</p>\n\n<pre><code>def process_exists(proc_name):\n    ps = subprocess.Popen("ps ax -o pid= -o args= ", shell=True, stdout=subprocess.PIPE)\n    ps_pid = ps.pid\n    output = ps.stdout.read()\n    ps.stdout.close()\n    ps.wait()\n\n    for line in output.split("\\n"):\n        res = re.findall("(\\d+) (.*)", line)\n        if res:\n            pid = int(res[0][0])\n            if proc_name in res[0][1] and pid != os.getpid() and pid != ps_pid:\n                return True\n    return False\n</code></pre>\n'}, {'answer_id': 11784942, 'author': 'mr.m', 'author_id': 1572451, 'author_profile': 'https://Stackoverflow.com/users/1572451', 'pm_score': 2, 'selected': False, 'text': '<p>i had problems with the versions above (for example the function found also part of the string and such things...)\nso i wrote my own, modified version of Maksym Kozlenko\'s:</p>\n\n<pre><code>#proc    -&gt; name/id of the process\n#id = 1  -&gt; search for pid\n#id = 0  -&gt; search for name (default)\n\ndef process_exists(proc, id = 0):\n   ps = subprocess.Popen("ps -A", shell=True, stdout=subprocess.PIPE)\n   ps_pid = ps.pid\n   output = ps.stdout.read()\n   ps.stdout.close()\n   ps.wait()\n\n   for line in output.split("\\n"):\n      if line != "" and line != None:\n        fields = line.split()\n        pid = fields[0]\n        pname = fields[3]\n\n        if(id == 0):\n            if(pname == proc):\n                return True\n        else:\n            if(pid == proc):\n                return True\nreturn False\n</code></pre>\n\n<p>I think it\'s more reliable, easier to read and you have the option to check for process ids or names.</p>\n'}, {'answer_id': 18259642, 'author': 'felbus', 'author_id': 236805, 'author_profile': 'https://Stackoverflow.com/users/236805', 'pm_score': 3, 'selected': False, 'text': '<p>I use this to get the processes, and the count of the process of the specified name</p>\n\n<pre><code>import os\n\nprocessname = \'somprocessname\'\ntmp = os.popen("ps -Af").read()\nproccount = tmp.count(processname)\n\nif proccount &gt; 0:\n    print(proccount, \' processes running of \', processname, \'type\')\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/38056', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1531/']