[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Support pid arguments in stem.util.system's is_running()



commit e212e4182bd74a7fbf8ccbca6625c6762f0d4b49
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Jun 4 11:08:13 2017 -0700

    Support pid arguments in stem.util.system's is_running()
    
    We have an is_running() function that accepts process names. Expanding it to be
    able to check for pids too...
    
      https://stackoverflow.com/a/568285
---
 docs/change_log.rst       |  1 +
 stem/util/system.py       | 14 ++++++++++++--
 test/integ/util/system.py | 12 ++++++++++--
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 926e7ea..84f7493 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -69,6 +69,7 @@ The following are only available within Stem's `git repository
   * Added timeout argument to :func:`~stem.util.system.call`
   * Added cwd argument to :func:`~stem.util.system.call`
   * Added :class:`~stem.util.test_tools.TimedTestRunner` and :func:`~stem.util.test_tools.test_runtimes`
+  * Supporing pid arguments in :func:`~stem.util.system.is_running`
 
  * **Interpreter**
 
diff --git a/stem/util/system.py b/stem/util/system.py
index 69ec979..6d96672 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -257,14 +257,24 @@ def is_available(command, cached=True):
 
 def is_running(command):
   """
-  Checks for if a process with a given name is running or not.
+  Checks for if a process with a given name or pid is running.
 
-  :param str command: process name to be checked
+  .. versionchanged:: 1.6.0
+     Added support for pid arguments.
+
+  :param str,int command: process name if a str or pid if an int to be checked
 
   :returns: **True** if the process is running, **False** if it's not among ps
     results, and **None** if ps can't be queried
   """
 
+  if isinstance(command, int):
+    try:
+      os.kill(command, 0)
+      return True
+    except OSError:
+      return False
+
   # Linux and the BSD families have different variants of ps. Guess based on
   # the is_bsd() check which to try first, then fall back to the other.
   #
diff --git a/test/integ/util/system.py b/test/integ/util/system.py
index 07d7d75..818b639 100644
--- a/test/integ/util/system.py
+++ b/test/integ/util/system.py
@@ -87,10 +87,18 @@ class TestSystem(unittest.TestCase):
 
     self.assertFalse(stem.util.system.is_available('blarg_and_stuff'))
 
+  def test_is_running_by_pid(self):
+    """
+    Checks the stem.util.system.is_running function with a pid.
+    """
+
+    self.assertTrue(stem.util.system.is_running(test.runner.get_runner().get_pid()))
+    self.assertFalse(stem.util.system.is_running(528955))
+
   @test.require.command('ps')
-  def test_is_running(self):
+  def test_is_running_by_name(self):
     """
-    Checks the stem.util.system.is_running function.
+    Checks the stem.util.system.is_running function with a process name.
     """
 
     # Check to see if the command we started tor with is running. The process



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits