summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastien Martini <seb@dbzteam.org>2010-09-15 01:08:07 +0200
committerSebastien Martini <seb@dbzteam.org>2010-09-15 01:08:07 +0200
commit909f985842fcae8a45d9591017112186e68a723d (patch)
tree7ef6a19285efd6100e88190a7fa6595922dfc5f9
parent155e2f5d0dfeff210198856b2b03d53d8e810c52 (diff)
downloadpyinotify-909f985842fcae8a45d9591017112186e68a723d.tar.gz
Optionally no pid file is written (pid_file=False) when notifier.loop
with daemonize=True is called.
-rw-r--r--python2/examples/daemon.py15
-rwxr-xr-xpython2/pyinotify.py26
-rwxr-xr-xpython3/pyinotify.py28
3 files changed, 45 insertions, 24 deletions
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/<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:
@@ -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:
diff --git a/python3/pyinotify.py b/python3/pyinotify.py
index 84fa17a..acee931 100755
--- a/python3/pyinotify.py
+++ b/python3/pyinotify.py
@@ -1163,7 +1163,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/<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:
@@ -1171,7 +1173,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)
@@ -1205,13 +1207,14 @@ 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, 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):
@@ -1241,7 +1244,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: