4.1. How to Build a Daemon

This section shows you how to build a daemon and offers a brief explanation of why each step is needed. Your application may not require all of the steps listed, and you may need to do them in a different order to meet your needs, but this will give you a general idea, nonetheless.

  1. Load the configuration.

  2. Go into the background.

  3. Become the process and session leader.

  4. Set the working directory.

  5. Redirect stdin, stdout, and stderr.

  6. Set up logging.

  7. Set group IDs and user IDs.

  8. Check for a pidfile.

  9. Set the umask.

  10. Set up signal handlers.

Note:

The sample daemon presented later in this chapter includes code for each of these steps. Some of the following sections use code taken from the sample daemon.

4.1.1. Load the Daemon's Configuration ...

Get Linux Appliance Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.