From 909f985842fcae8a45d9591017112186e68a723d Mon Sep 17 00:00:00 2001 From: Sebastien Martini Date: Wed, 15 Sep 2010 01:08:07 +0200 Subject: Optionally no pid file is written (pid_file=False) when notifier.loop with daemonize=True is called. --- python2/examples/daemon.py | 15 ++++++++++----- python2/pyinotify.py | 26 +++++++++++++++++--------- 2 files changed, 27 insertions(+), 14 deletions(-) (limited to 'python2') diff --git a/python2/examples/daemon.py b/python2/examples/daemon.py index aeb5249..e1e9ac1 100644 --- a/python2/examples/daemon.py +++ b/python2/examples/daemon.py @@ -36,8 +36,13 @@ on_loop_func = functools.partial(on_loop, counter=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 diff --git a/python2/pyinotify.py b/python2/pyinotify.py index 9c0aafc..32e2f22 100755 --- a/python2/pyinotify.py +++ b/python2/pyinotify.py @@ -1202,7 +1202,9 @@ class Notifier: 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/.pid, if + pid_file=False no pid_file is written. stdin, stdout, stderr: files associated to common streams. """ if pid_file is None: @@ -1210,7 +1212,7 @@ class Notifier: 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) @@ -1244,12 +1246,13 @@ class Notifier: 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): @@ -1279,7 +1282,12 @@ class Notifier: @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: -- cgit v1.2.1