title: Python-daemon how it works
body: <p>I'm stuck and need help.</p>
  
  <p>I'm using python-daemon package to daemonize a program. The problem is that I don't know how to start and stop the daemon.</p>
  
  <p>When I run</p>
  
  <pre><code>python myscript.py start
  </code></pre>
  
  <p>A new process is created. However when I run the stop nothing happens.</p>
  
  <pre><code># Setting logging configuration
  logger = logging.getLogger("MyScript")
  
  logger.setLevel(logging.DEBUG)
  
  formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
  handler = logging.FileHandler("/tmp/myscript.log")
  handler.setFormatter(formatter)
  logger.addHandler(handler)
  
  pid = daemon.pidlockfile.TimeoutPIDLockFile("/tmp/myscript.pid", 10)
  context = daemon.DaemonContext(
      #working_directory='/var/lib/foo',
      umask=0o002,
      pidfile=pid,
      files_preserve=[handler.stream],
      )
  
  loop = True
  def program_cleanup_test():
      logger.info("Stopping loop")
      loop = False
  
  context.signal_map = {
      signal.SIGTERM: program_cleanup_test,
      signal.SIGHUP: 'terminate',
      #signal.SIGUSR1: reload_program_config,
      }
  
  print "Running as a daemon"
  with context:
      while loop:
          logger.info("0255")
          time.sleep(5)
  </code></pre>