Python 2.7 使用pyinotify作为守护进程启动脚本

Python 2.7 使用pyinotify作为守护进程启动脚本,python-2.7,ubuntu-12.04,daemon,pyinotify,Python 2.7,Ubuntu 12.04,Daemon,Pyinotify,关于使用pyinotify作为守护进程启动脚本,我有很多问题 我有一些这样的代码: #!/usr/bin/env python import sys import pyinotify import shutil import glob PACKAGES_DIR = '/var/my-packages' PACKAGES_TEMP_DIR = '/var/www-data/package_temp' wm = pyinotify.WatchManager() mask = pyinotify

关于使用pyinotify作为守护进程启动脚本,我有很多问题

我有一些这样的代码:

#!/usr/bin/env python

import sys
import pyinotify
import shutil
import glob

PACKAGES_DIR = '/var/my-packages'
PACKAGES_TEMP_DIR = '/var/www-data/package_temp'

wm = pyinotify.WatchManager()
mask = pyinotify.IN_MOVED_TO

class ProcessPackages(pyinotify.ProcessEvent):
    def process_IN_MOVED_TO(self, event):
        for directory in glob.glob(PACKAGES_TEMP_DIR + '/*'):
            shutil.move(directory, PACKAGES_DIR)

handler = ProcessPackages()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(PACKAGES_TEMP_DIR, mask)

try:
    notifier.loop(daemonize=True, pid_file='/tmp/packages.pid',  stdout='/tmp/stdout.txt')
except pyinotify.NotifierError, err:
    print >> sys.stderr, err
我现在的问题是,如果我将daemonize参数设置为True,这是否意味着整个脚本作为守护进程运行,还是仅仅是pyinotify

如果只是pyinotify,我将如何作为守护进程运行整个脚本

如果我以守护进程的形式运行脚本,那么让pyinotify也成为守护进程真的有必要吗

我的最后一个问题是,如果pyinotify被守护,我肯定需要回调吗?在我的例子中,我只希望脚本永远运行,并且只在系统重新启动/重新启动时被终止

该脚本还应该像任何标准启动脚本一样运行,无需手动干预

仅供参考

我正在运行Ubuntu 12.04服务器

提前感谢,,
nav

我使用Upstart(同样在Ubuntu 12.04上)作为系统服务运行依赖于ipynotify的进程(这是您想要的)

就我个人而言,我根本没有修改python脚本。我只是确保它在终端上正常运行,然后创建了一个upstart配置文件,如下所示:

/etc/init/myservice.conf:

当init文件就位后,您需要尝试类似于
sudo start myservice
的操作,然后检查/tmp/myscript.log是否有任何错误


谢谢你的回答,由于最后期限的关系,我不得不使用start-stop-daemon方法,但我肯定会在另一个案例中尝试这种方法。
description "MyService"
author "My Name"

start on runlevel [2345]
stop on runlevel [!2345]

# Automatically restart process if crashed
#respawn

exec su myuser -c "/usr/bin/python /path/to/myscript.py > /tmp/myscript.log 2>&1"