0

我有很多关于使用 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 服务器。

在此先感谢,导航

4

1 回答 1

1

我使用 Upstart 运行一个依赖于 ipynotify 的进程作为系统服务(从事物的声音来看,这是你想要的)——也在 Ubuntu 12.04 上。

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

/etc/init/myservice.conf

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"

当你的 init 文件就位后,你会想尝试类似的东西sudo start myservice,然后检查/tmp/myscript.log是否有任何错误。

于 2014-06-04T14:03:54.087 回答