title: Proper way to write a daemon in 2019 in Python
body: <p><strong>TL;DR;</strong></p>
  
  <p>I would like to write a daemon in Python, but I feel that PEP 3143 is overkill now that almost everybody uses <code>systemd</code>. I am looking for advice for a good start to write a daemon in Python.</p>
  
  <p><strong>Context</strong></p>
  
  <p>By reading other related questions on SO, it seems there is a before and an after <code>systemd</code>. The articles I have read are more than 10 years old and I feel that nowadays it is much simpler to achieve what I want to do.</p>
  
  <p>I would like to write a program container that can be run either: </p>
  
  <ul>
  <li>In the front (blocking) (<code>$ ./foo</code>)</li>
  <li>In the background (<code>$ ./foo &amp;</code>)</li>
  <li>In a detached state (<code>$ ./foo start</code>, <code>$ ./foo stop</code>).</li>
  <li>Managed by <code>systemd</code> (<code>$ sudo systemctl start foo</code>)</li>
  </ul>
  
  <p>Being able to <code>start</code> and <code>stop</code> the program by itself would require these commands: </p>
  
  <pre><code>$ daemon start
  $ daemon stop
  $ daemon status
  </code></pre>
  
  <p>Also if the program is able to demonize itself it would also take care of some side effects (prevent zombies, double fork, pidfile, logging...) </p>
  
  <p>I have yet not figured out how to manage the log. Since a daemon is detached from a TTY, it should redirect <code>stdin</code>, <code>stdout</code>, <code>stderr</code> to <code>/dev/null</code> and use a logger instead. To use a logger I can see different options: </p>
  
  <ol>
  <li>Use Syslog, but require a write access to <code>/dev/log</code></li>
  <li>Use <code>daemon.log</code> through <code>stdout</code> and <code>systemd</code>, but requires <code>systemd</code></li>
  <li>Use a custom log file that will be specified with <code>--log=~/foo.log</code></li>
  <li>Use <code>stdout</code>/<code>stderr</code> because the process is not detached</li>
  </ol>
  
  <p>For the PID file, the traditional location is in <code>/var/run/</code> in which most users have no access. So the user should be able to configure the <code>--pidfile</code>. </p>
  
  <p>From this I realize that building a simple daemon is not an easy task and I do not know from where to start. </p>
  
  <p>One trivial approach would be to have two separated programs. One simple blocking program that performs the task, use <code>stdout</code> and one process manager that can do what <code>systemd</code> do, but at a user level. </p>
  
  <p>If I would summarize my question in one sentence I would say: </p>
  
  <blockquote>
    <p>Is it worth it to use <code>PEP3143 Standard daemon process library</code> in 2019 to write a daemon in Python instead of relying on a daemon manager such as <code>systemd</code>?</p>
  </blockquote>