Skip to content

Instantly share code, notes, and snippets.

@VigneshChennai
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save VigneshChennai/35bb64d41e4a4beb2225 to your computer and use it in GitHub Desktop.

Select an option

Save VigneshChennai/35bb64d41e4a4beb2225 to your computer and use it in GitHub Desktop.

Revisions

  1. VigneshChennai revised this gist Feb 19, 2015. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions PyDaemonize
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@

    import os

    def daemonize(log_folder):
    def daemonize(log_folder, wd="/", umask=0):
    try:
    f = os.fork()
    except OSError as e:
    @@ -27,22 +27,21 @@ def daemonize(log_folder):
    if f != 0:
    sys.exit(0)
    else:
    signal.signal(signal.SIGHUP,
    lambda x, y: print("SIGHUP received during process forking, ignoring signal"))
    os.chdir("/")
    signal.signal(signal.SIGHUP, lambda x, y: print("SIGHUP received during process forking, ignoring signal"))
    os.chdir(wd)
    os.setsid()
    os.umask(0)
    os.umask(umask)
    try:
    f = os.fork()
    except OSError as e:
    print("SEVERE: Error forking.. <{fork}>".format(fork=e))
    print("SEVERE: Trace -> {trace}".format(
    trace=traceback.format_exc()))
    raise e from None

    if f != 0:
    sys.exit(0)

    print("INFO: Started as Daemon")
    r = open("/dev/null", "r")
    os.dup2(r.fileno(), 0)
  2. VigneshChennai created this gist Feb 19, 2015.
    52 changes: 52 additions & 0 deletions PyDaemonize
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/usr/bin/env python

    # PyDaemonize is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.

    # PyDaemonize is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.

    # You should have received a copy of the GNU General Public License
    # along with PyDaemonize. If not, see <http://www.gnu.org/licenses/>.

    import os

    def daemonize(log_folder):
    try:
    f = os.fork()
    except OSError as e:
    print("SEVERE: Error forking.. <{fork}>".format(fork=e))
    print("SEVERE: Trace -> {trace}".format(
    trace=traceback.format_exc()))
    raise e from None

    if f != 0:
    sys.exit(0)
    else:
    signal.signal(signal.SIGHUP,
    lambda x, y: print("SIGHUP received during process forking, ignoring signal"))
    os.chdir("/")
    os.setsid()
    os.umask(0)
    try:
    f = os.fork()
    except OSError as e:
    print("SEVERE: Error forking.. <{fork}>".format(fork=e))
    print("SEVERE: Trace -> {trace}".format(
    trace=traceback.format_exc()))
    raise e from None

    if f != 0:
    sys.exit(0)

    print("INFO: Started as Daemon")
    r = open("/dev/null", "r")
    os.dup2(r.fileno(), 0)
    buffering = 1 # line buffering
    w = open(log_folder, "w", buffering=buffering)
    os.dup2(w.fileno(), 1)
    os.dup2(w.fileno(), 2)