Giter Site home page Giter Site logo

loopy's Introduction

Simple Daemon Scripts

Recipe for creating a Linux daemon from a Python, Ruby, or NodeJS script.

Service managers like runit, SysV init, and upstart work great to start daemons but it can be tricky to stop daemons. The requirements of a well behaved daemon are pretty onerous to add to your script. The python-daemon package can handle this for you, but I prefer to keep the daemon logic out of my script as much as possible.

This repo is a demo using runit to run a python script as a daemon that can be cleanly started and stopped and is automatically restarted when it exits. The python script is not burdened with daemon logic.

loopy.py

Example daemon that just prints from time to time. It knows very little about its daemonic nature: it writes its pid to a file, that's it.

runit setup

runit is simple and reliable. It restarts your daemon automatically if it exits, which is really useful. This frees your daemon from having complicated try/catch logic that tries to keep it running at all costs while still reporting errors. With runit, just let your daemon exit if it hits a hard error: runit will restart it.

/etc/service/loopy/run

This starts your daemon and sends its standard output to the system logger.

/etc/service/loopy/control/t

This is a runit hook script that is called whenever your service is stopped. Why do we need it? The loopy daemon runs as two processes, and runit only knows about the parent process that it started. The control/t script stops the child python process cleanly.

Control hooks are somewhat buried in the docs, but basically runit lets you run a custom script for each action it takes on a daemon.

/etc/service/loopy/control/u

This hook is run when your service starts. It is a symlink to t. It tries to the process and pid file if it is already running. It almost never will be running, so u is mostly an extra precaution.

That's it. You can see it in action with docker or use the snippets above in your own daemons.

Running Demo in Docker

  • docker build -t loopy --rm .
  • docker run -it loopy /sbin/my_init -- bash -l

You'll see the loopy output every few seconds. After 25 seconds loopy will exit and be restarted automatically. You can do sv start loopy and sv stop loopy. You can also do ps -ef to see that there is one loopy python process when it is started and zero loopy processes when it is stopped.

References

License

MIT License

loopy's People

Contributors

mgk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.