module Daemons

All functions and classes that Daemons provides reside in this module.

Daemons is normally invoked by one of the following four ways:

  1. Daemons.run(script, options): This is used in wrapper-scripts that are supposed to control other ruby scripts or external applications. Control is completely passed to the daemons library. Such wrapper script need to be invoked with command line options like ‘start’ or ‘stop’ to do anything useful.

  2. Daemons.run_proc(app_name, options) { (...) }: This is used in wrapper-scripts that are supposed to control a proc. Control is completely passed to the daemons library. Such wrapper scripts need to be invoked with command line options like ‘start’ or ‘stop’ to do anything useful.

  3. Daemons.call(options) { block }: Execute the block in a new daemon. Daemons.call will return immediately after spawning the daemon with the new Application object as a return value.

  4. Daemons.daemonize(options): Daemonize the currently runnig process, i.e. the calling process will become a daemon.

What does daemons internally do with my daemons?

or

why do my daemons crash when they try to open a file?

or

why can I not see any output from the daemon on the console (when using for example puts)?

From a technical aspect of view, daemons does the following when creating a daemon:

  1. Forks a child (and exits the parent process, if needed)

  2. Becomes a session leader (which detaches the program from the controlling terminal).

  3. Forks another child process and exits first child. This prevents the potential of acquiring a controlling terminal.

  4. Changes the current working directory to “/”.

  5. Clears the file creation mask (sets umask to 0000).

  6. Closes file descriptors (reopens +$stdout+ and +$stderr+ to point to a logfile if possible).

So what does this mean for your daemons:

How do PidFiles work? Where are they stored?

Also, you are maybe interested in reading the documentation for the class PidFile. There you can find out about how Daemons works internally and how and where the so called PidFiles are stored.