Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to daemonize your process? (v2014)

How to daemonize your process? (v2014)

Yamashita, Yuu

August 07, 2014
Tweet

More Decks by Yamashita, Yuu

Other Decks in Technology

Transcript

  1. 1. What’s the daemons? Services on *nix systems * Background

    processes of the system * Detached from control terminal * Direct children of init(1)
  2. A traditional daemon: double fork if os.fork(): os.setsid() pid =

    os.fork() if pid == 0: main(sys.argv[1:]) else: with open(PIDFILE, “w”) as fp: fp.write(“%d” % (pid,))
  3. 2. How to create daemons? There’re some sort of init(1).

    2-1. sysvinit 2-2. daemontools 2-3. upstart 2-4. systemd
  4. 2-1. sysvinit /etc/init.d/* * Traditional; originally from UNIX SystemV *

    Double forking process * Write PID to a file * Read PID from the file when stopping it * Do nothing even if the process has died
  5. 2-2. daemontools /etc/service, /service * Written by djb; djbdns, qmail

    * Completely incompatible with sysvinit * No need for double forking and PID files * Automatically restart the process if died * Support logging (multilog) * No longer actively developed
  6. 2-3. upstart (>= Ubuntu 6.10) /etc/init * Originally written by

    Canonical (Ubuntu) * Compatible with sysvinit * Parallel execution for faster boot up * No need for double forking and PID files * Poor support for logging * No longer actively developed
  7. 2-4. systemd (>= Ubuntu 14.10?) /etc/systemd, /usr/lib/systemd * Originally written

    by RedHat * Compatible with sysvinit * Parallel execution for faster boot up * No need for double forking and PID files * Support logging (journalctl) * Actively developing
  8. 3. How to choose one? systemd >>> upstart >>> daemontools

    >>> ... * Choose one supported on your platform * Daemontools is complicated for newbies * Upstart is deprecated even on Ubuntu
  9. 4. Conclusion * Use systemd, upstart or daemontools * Modern

    init supports daemonizing from forground process (No need for double forking and PID files) * Modern init supports respawn of the process