<filename>ulutil/daemonize.py
# Copyright 2014 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# from Python Cookbook, 2nd edition, Recipe 9.13
import sys, os
''' Module to fork the current process as a daemon.
    NOTE: don't do any of this if your daemon gets started by inetd!  inetd
    does all you need, including redirecting standard file descriptors;
    the chdir( ) and umask( ) steps are the only ones you may still want.
'''
def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
    ''' Fork the current process as a daemon, redirecting standard file
        descriptors (by default, redirects them to /dev/null).
    '''
    # Perform first fork.
    try:
        pid = os.fork( )
        if pid > 0:
            sys.exit(0) # Exit first parent.
    except OSError, e:
        sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # Decouple from parent environment.
    os.chdir("/")
    os.umask(0)
    os.setsid( )
    # Perform second fork.
    try:
        pid = os.fork( )
        if pid > 0:
            sys.exit(0) # Exit second parent.
    except OSError, e:
        sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # The process is now daemonized, redirect standard file descriptors.
    for f in sys.stdout, sys.stderr: f.flush( )
    si = file(stdin, 'r')
    so = file(stdout, 'a+')
    se = file(stderr, 'a+', 0)
    os.dup2(si.fileno( ), sys.stdin.fileno( ))
    os.dup2(so.fileno( ), sys.stdout.fileno( ))
    os.dup2(se.fileno( ), sys.stderr.fileno( ))

# def _example_main ( ):
#     ''' Example main function: print a count & timestamp each second '''
#     import time
#     sys.stdout.write('Daemon started with pid %d\n' % os.getpid( ) )
#     sys.stdout.write('Daemon stdout output\n')
#     sys.stderr.write('Daemon stderr output\n')
#     c = 0
#     while True:
#         sys.stdout.write('%d: %s\n' % (c, time.ctime( )))
#         sys.stdout.flush( )
#         c = c + 1
#         time.sleep(1)
# if _ _name_ _ == "_ _main_ _":
#     daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log')
#     _example_main( )

<filename>ulutil/daemonize.py
# Copyright 2014 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# from Python Cookbook, 2nd edition, Recipe 9.13
import sys, os
''' Module to fork the current process as a daemon.
    NOTE: don't do any of this if your daemon gets started by inetd!  inetd
    does all you need, including redirecting standard file descriptors;
    the chdir( ) and umask( ) steps are the only ones you may still want.
'''
def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
    ''' Fork the current process as a daemon, redirecting standard file
        descriptors (by default, redirects them to /dev/null).
    '''
    # Perform first fork.
    try:
        pid = os.fork( )
        if pid > 0:
            sys.exit(0) # Exit first parent.
    except OSError, e:
        sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # Decouple from parent environment.
    os.chdir("/")
    os.umask(0)
    os.setsid( )
    # Perform second fork.
    try:
        pid = os.fork( )
        if pid > 0:
            sys.exit(0) # Exit second parent.
    except OSError, e:
        sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    # The process is now daemonized, redirect standard file descriptors.
    for f in sys.stdout, sys.stderr: f.flush( )
    si = file(stdin, 'r')
    so = file(stdout, 'a+')
    se = file(stderr, 'a+', 0)
    os.dup2(si.fileno( ), sys.stdin.fileno( ))
    os.dup2(so.fileno( ), sys.stdout.fileno( ))
    os.dup2(se.fileno( ), sys.stderr.fileno( ))

# def _example_main ( ):
#     ''' Example main function: print a count & timestamp each second '''
#     import time
#     sys.stdout.write('Daemon started with pid %d\n' % os.getpid( ) )
#     sys.stdout.write('Daemon stdout output\n')
#     sys.stderr.write('Daemon stderr output\n')
#     c = 0
#     while True:
#         sys.stdout.write('%d: %s\n' % (c, time.ctime( )))
#         sys.stdout.flush( )
#         c = c + 1
#         time.sleep(1)
# if _ _name_ _ == "_ _main_ _":
#     daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log')
#     _example_main( )

# Copyright 2014 <NAME> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from Python Cookbook, 2nd edition, Recipe 9.13 Module to fork the current process as a daemon.     NOTE: don't do any of this if your daemon gets started by inetd!  inetd     does all you need, including redirecting standard file descriptors;     the chdir( ) and umask( ) steps are the only ones you may still want. Fork the current process as a daemon, redirecting standard file         descriptors (by default, redirects them to /dev/null). # Perform first fork. # Exit first parent. #1 failed: (%d) %s\n" % (e.errno, e.strerror)) # Decouple from parent environment. # Perform second fork. # Exit second parent. #2 failed: (%d) %s\n" % (e.errno, e.strerror)) # The process is now daemonized, redirect standard file descriptors. # def _example_main ( ): #     ''' Example main function: print a count & timestamp each second ''' #     import time #     sys.stdout.write('Daemon started with pid %d\n' % os.getpid( ) ) #     sys.stdout.write('Daemon stdout output\n') #     sys.stderr.write('Daemon stderr output\n') #     c = 0 #     while True: #         sys.stdout.write('%d: %s\n' % (c, time.ctime( ))) #         sys.stdout.flush( ) #         c = c + 1 #         time.sleep(1) # if _ _name_ _ == "_ _main_ _": #     daemonize('/dev/null','/tmp/daemon.log','/tmp/daemon.log') #     _example_main( )