2016-07-21 2 views
0

Я пытаюсь добавить часы, используя python pyinotify и daemonize уведомитель.Как добавить файл stdout в файл для pyinotify daemonize?

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

Однако, я мог найти, файлы журнала перезаписываются, не добавляя их. Есть ли способ добавить stdout и stderr? Я нахожусь в ОС Linux.

благодаря

ответ

0

Глядя на the source, кажется, будто журналы открываются в 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)) 

Там является pull request открыт для открытия журналов в режиме O_APPEND, но это почти 8 месяцев, и автор не ответил.

Не похоже, что есть разумный способ добавления журналов в это время.

+0

ОК .. Спасибо .. Я переименую файл журнала с отметкой времени перед вызовом цикла. – SKRP

Смежные вопросы