Skip to content

Commit

Permalink
Optionally no pid file is written (pid_file=False) when notifier.loop
Browse files Browse the repository at this point in the history
with daemonize=True is called.
  • Loading branch information
seb-m committed Sep 14, 2010
1 parent 155e2f5 commit 909f985
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
15 changes: 10 additions & 5 deletions python2/examples/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ def on_loop(notifier, counter):
# Notifier instance spawns a new process when daemonize is set to True. This
# child process' PID is written to /tmp/pyinotify.pid (it also automatically
# deletes it when it exits normally). If no custom pid_file is provided it
# would write it more traditionally under /var/run/. /tmp/stdout.txt is used
# as stdout stream thus traces of events will be written in it. callback is
# the above function and will be called after each event loop.
notifier.loop(daemonize=True, callback=on_loop_func,
pid_file='/tmp/pyinotify.pid', stdout='/tmp/stdout.txt')
# would write it more traditionally under /var/run/. Note that in both cases
# the caller must ensure the pid file doesn't exist when this method is called
# othewise it will raise an exception. /tmp/stdout.txt is used as stdout
# stream thus traces of events will be written in it. callback is the above
# function and will be called after each event loop.
try:
notifier.loop(daemonize=True, callback=on_loop_func,
pid_file='/tmp/pyinotify.pid', stdout='/tmp/stdout.txt')
except pyinotify.NotifierError, err:
print >> sys.stderr, err
26 changes: 17 additions & 9 deletions python2/pyinotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,15 +1202,17 @@ def process_events(self):
def __daemonize(self, pid_file=None, stdin=os.devnull, stdout=os.devnull,
stderr=os.devnull):
"""
pid_file: file to which the pid will be written.
pid_file: file where the pid will be written. If pid_file=None the pid
is written to /var/run/<sys.argv[0]|pyinotify>.pid, if
pid_file=False no pid_file is written.
stdin, stdout, stderr: files associated to common streams.
"""
if pid_file is None:
dirname = '/var/run/'
basename = os.path.basename(sys.argv[0]) or 'pyinotify'
pid_file = os.path.join(dirname, basename + '.pid')

if os.path.lexists(pid_file):
if pid_file != False and os.path.lexists(pid_file):
err = 'Cannot daemonize: pid file %s already exists.' % pid_file
raise NotifierError(err)

Expand Down Expand Up @@ -1244,12 +1246,13 @@ def fork_daemon():
fork_daemon()

# Write pid
flags = os.O_WRONLY|os.O_CREAT|os.O_NOFOLLOW|os.O_EXCL
fd_pid = os.open(pid_file, flags, 0600)
os.write(fd_pid, str(os.getpid()) + '\n')
os.close(fd_pid)

atexit.register(lambda : os.unlink(pid_file))
if pid_file != False:
flags = os.O_WRONLY|os.O_CREAT|os.O_NOFOLLOW|os.O_EXCL
fd_pid = os.open(pid_file, flags, 0600)
os.write(fd_pid, str(os.getpid()) + '\n')
os.close(fd_pid)
# Register unlink function
atexit.register(lambda : os.unlink(pid_file))


def _sleep(self, ref_time):
Expand Down Expand Up @@ -1279,7 +1282,12 @@ def loop(self, callback=None, daemonize=False, **args):
@type daemonize: boolean
@param args: Optional and relevant only if daemonize is True. Remaining
keyworded arguments are directly passed to daemonize see
__daemonize() method.
__daemonize() method. If pid_file=None or is set to a
pathname the caller must ensure the file does not exist
before this method is called otherwise an exception
pyinotify.NotifierError will be raised. If pid_file=False
it is still daemonized but the pid is not written in any
file.
@type args: various
"""
if daemonize:
Expand Down
28 changes: 18 additions & 10 deletions python3/pyinotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,15 +1163,17 @@ def process_events(self):
def __daemonize(self, pid_file=None, stdin=os.devnull, stdout=os.devnull,
stderr=os.devnull):
"""
pid_file: file to which the pid will be written.
pid_file: file where the pid will be written. If pid_file=None the pid
is written to /var/run/<sys.argv[0]|pyinotify>.pid, if
pid_file=False no pid_file is written.
stdin, stdout, stderr: files associated to common streams.
"""
if pid_file is None:
dirname = '/var/run/'
basename = os.path.basename(sys.argv[0]) or 'pyinotify'
pid_file = os.path.join(dirname, basename + '.pid')

if os.path.lexists(pid_file):
if pid_file != False and os.path.lexists(pid_file):
err = 'Cannot daemonize: pid file %s already exists.' % pid_file
raise NotifierError(err)

Expand Down Expand Up @@ -1205,13 +1207,14 @@ def fork_daemon():
fork_daemon()

# Write pid
flags = os.O_WRONLY|os.O_CREAT|os.O_NOFOLLOW|os.O_EXCL
fd_pid = os.open(pid_file, flags, 0o0600)
os.write(fd_pid, bytes(str(os.getpid()) + '\n',
locale.getpreferredencoding()))
os.close(fd_pid)

atexit.register(lambda : os.unlink(pid_file))
if pid_file != False:
flags = os.O_WRONLY|os.O_CREAT|os.O_NOFOLLOW|os.O_EXCL
fd_pid = os.open(pid_file, flags, 0o0600)
os.write(fd_pid, bytes(str(os.getpid()) + '\n',
locale.getpreferredencoding()))
os.close(fd_pid)
# Register unlink function
atexit.register(lambda : os.unlink(pid_file))


def _sleep(self, ref_time):
Expand Down Expand Up @@ -1241,7 +1244,12 @@ def loop(self, callback=None, daemonize=False, **args):
@type daemonize: boolean
@param args: Optional and relevant only if daemonize is True. Remaining
keyworded arguments are directly passed to daemonize see
__daemonize() method.
__daemonize() method. If pid_file=None or is set to a
pathname the caller must ensure the file does not exist
before this method is called otherwise an exception
pyinotify.NotifierError will be raised. If pid_file=False
it is still daemonized but the pid is not written in any
file.
@type args: various
"""
if daemonize:
Expand Down

0 comments on commit 909f985

Please sign in to comment.