2

I've been following the this example to implement a python daemon, and it seems to be somewhat working, but only the reconfigure function is called.

This is the code I've been using:

import signal
import daemon
import lockfile

import manager

context = daemon.DaemonContext(
    working_directory='/home/debian/station',
    pidfile=lockfile.FileLock('/var/run/station.pid'))

context.signal_map = {
    signal.SIGTERM: manager.Manager.program_terminate,
    signal.SIGHUP: 'terminate',
    signal.SIGUSR1: manager.Manager.program_reload_configuration,
    }

manager.Manager.program_configure()

with context:
    manager.Manager.program_start()

This is the code on the manager class:

@staticmethod
def program_configure():
    logging.info('Configuring program')

@staticmethod
def program_reload_configuration():
    logging.info('Reloading configuration')

@staticmethod
def program_start():
    global Instance
    logging.info('Program started')
    Instance = Manager()
    Instance.run()

@staticmethod
def program_terminate():
    logging.info('Terminating')

And the log shows only:

INFO:root:Configuring program

For some reason program_start() isn't being called. program_configure() is called every time the python file is read, so that's that, but why isn't program_start() called?

I start the daemon by typing sudo service station.sh start and the line that runs the script is:

python $DAEMON start

EDIT: After reading a bit, I've realized that the program probably exits or hangs in context.__enter__() (with calls that). But I have no clue what is causing this

Nitay
  • 4,193
  • 6
  • 33
  • 42

1 Answers1

1

The problem wasn't in the python-daemon not calling the functions, it's the logging that didn't work.

When the daemon creates a new process it doesn't transfer all file handles from the mother process - Therefore the logs aren't written. See this question for more info.

The solution to that is to use the files_preserve property like so:

# Set the logger
LOG_LEVEL = logging.DEBUG
logger = logging.getLogger()
logger.setLevel(LOG_LEVEL)
fh = logging.FileHandler(LOG_FILENAME)
logger.addHandler(fh)

# Not create the context, and notify it to preserve the log file
context = daemon.DaemonContext(
    working_directory='/home/debian/station',
    pidfile=lockfile.FileLock('/var/run/station.pid'),
    files_preserve=[fh.stream],
)
Community
  • 1
  • 1
Nitay
  • 4,193
  • 6
  • 33
  • 42