简体   繁体   English

如何将stdout附加到文件以pyinotify守护进程?

[英]How to append stdout to file for pyinotify daemonize?

I am trying to add watches using python pyinotify and daemonize the notifier. 我正在尝试使用python pyinotify添加手表并守护通告程序。

notifier = pyinotify.Notifier(wm, handler)
notifier.loop(daemonize=True, pid_file='/tmp/pyinotifier.pid',
              stdout='/tmp/out.log', stderr='/tmp/error.log')

However I could find, the log files are overwritten not getting appended. 但是我发现,日志文件被覆盖,没有附加。 Is there a way to append stdout and stderr ? 有没有办法附加stdoutstderr I am on Linux OS. 我在Linux OS上。

thanks 谢谢

Looking at the source , it appears as though the logs are opened in O_WRONLY mode, which is why you see them being overwritten each time you invoke the loop: 查看源代码 ,看起来好像是在O_WRONLY模式下打开了日志,这就是为什么您每次调用循环都会看到它们被覆盖的原因:

def __daemonize(self, pid_file=None, stdin=os.devnull, stdout=os.devnull,
                stderr=os.devnull):
    """
    @param 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.
    @param stdin:
    @param stdout:
    @param 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 pid_file != False and os.path.lexists(pid_file):
        err = 'Cannot daemonize: pid file %s already exists.' % pid_file
        raise NotifierError(err)

    def fork_daemon():
        # Adapted from Chad J. Schroeder's recipe
        # @see http://code.activestate.com/recipes/278731/
        pid = os.fork()
        if (pid == 0):
            # parent 2
            os.setsid()
            pid = os.fork()
            if (pid == 0):
                # child
                os.chdir('/')
                os.umask(022)
            else:
                # parent 2
                os._exit(0)
        else:
            # parent 1
            os._exit(0)

        fd_inp = os.open(stdin, os.O_RDONLY)
        os.dup2(fd_inp, 0)
        fd_out = os.open(stdout, os.O_WRONLY|os.O_CREAT, 0600)
        os.dup2(fd_out, 1)
        fd_err = os.open(stderr, os.O_WRONLY|os.O_CREAT, 0600)
        os.dup2(fd_err, 2)

    # Detach task
    fork_daemon()

    # Write pid
    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))

There is a pull request open to open the logs in O_APPEND mode, but it's nearly 8 months old and the author has not responded. 打开了一个拉取请求 ,以O_APPEND模式打开日志,但是已经有将近8个月的时间了,作者没有响应。

It doesn't look like there's a reasonable way to append the logs at this time. 目前似乎没有合理的方法来添加日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM