John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/190696

Change subject: Import daemonize from compat
......................................................................

Import daemonize from compat

Minor pep changes.

Bug: T89581
Change-Id: I8daac25c55ad4961b75641b44edbd218760d9186
---
A pywikibot/daemonize.py
M tox.ini
2 files changed, 64 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/96/190696/1

diff --git a/pywikibot/daemonize.py b/pywikibot/daemonize.py
new file mode 100644
index 0000000..d0b5ae5
--- /dev/null
+++ b/pywikibot/daemonize.py
@@ -0,0 +1,63 @@
+# -*- coding: utf-8  -*-
+"""Module to daemonize the current process on Unix."""
+#
+# (C) Pywikibot team, 2007-2013
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+#
+
+import os
+import sys
+
+is_daemon = False
+
+
+def daemonize(close_fd=True, chdir=True, write_pid=False, redirect_std=None):
+    """
+    Daemonize the current process.
+
+    Only works on POSIX compatible operating systems.
+    The process will fork to the background and return control to the terminal.
+
+    Arguments:
+        - close_fd: Close the standard streams and replace them by /dev/null
+        - chdir: Change the current working directory to /
+        - write_pid: Write the pid to sys.argv[0] + '.pid'
+        - redirect_std: Filename to redirect stdout and stdin to
+    """
+    # Fork away
+    if not os.fork():
+        # Become session leader
+        os.setsid()
+        # Fork again to prevent the process from acquiring a
+        # controlling terminal
+        pid = os.fork()
+        if not pid:
+            global is_daemon
+            is_daemon = True
+
+            if close_fd:
+                os.close(0)
+                os.close(1)
+                os.close(2)
+                os.open('/dev/null', os.O_RDWR)
+                if redirect_std:
+                    os.open(redirect_std, os.O_WRONLY | os.O_APPEND)
+                else:
+                    os.dup2(0, 1)
+                os.dup2(1, 2)
+            if chdir:
+                os.chdir('/')
+            return
+        else:
+            # Write out the pid
+            f = open(os.path.basename(sys.argv[0]) + '.pid', 'w')
+            f.write(str(pid))
+            f.close()
+            os._exit(0)
+    else:
+        # Exit to return control to the terminal
+        # os._exit to prevent the cleanup to run
+        os._exit(0)
diff --git a/tox.ini b/tox.ini
index da80421..80ee993 100644
--- a/tox.ini
+++ b/tox.ini
@@ -42,6 +42,7 @@
     pywikibot/comms/rcstream.py \
     pywikibot/compat/ \
     pywikibot/config2.py \
+    pywikibot/daemonize.py \
     pywikibot/data/__init__.py \
     pywikibot/data/api.py \
     pywikibot/data/wikistats.py \

-- 
To view, visit https://gerrit.wikimedia.org/r/190696
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8daac25c55ad4961b75641b44edbd218760d9186
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>
Gerrit-Reviewer: btongminh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to