Question

I have a number of questions regarding starting a script using pyinotify as a daemon.

I have some code like this:

#!/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

My question now is if I set to the daemonize parameter to True does this mean that the whole script is run as daemon or is it just pyinotify?

If it is only pyinotify how would I go about running this entire script as a daemon process?

If I run the script as daemon is it really necessary to make pyinotify a daemon as well?

My last question is if pyinotify is daemonized would I definitely need a callback? In my case I just want the script to run forever and being killed only on system reboot/restart.

The script should also run like any standard startup script without manual intervention.

FYI,

I am running an Ubuntu 12.04 server.

Thanks in advance, nav

Was it helpful?

Solution

I run an ipynotify-dependent process as a system service (which is what you want, by the sound of things), using Upstart - also on Ubuntu 12.04.

Personally, I didn't modify the python script at all. I just made sure it ran fine at the terminal, then created an upstart config file like so:

/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"

When your init file is in place, you'll want to try something like sudo start myservice, then inspect /tmp/myscript.log for any errors.

HTH!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top