The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Proc::Daemon - Run Perl program as a daemon process

SYNOPSIS

use Proc::Daemon;
Proc::Daemon::Init;

DESCRIPTION

This module contains the routine Init which can be called by a Perl program to initialize itself as a daemon. A daemon is a process that runs in the background with no controlling terminal. Generally servers (like FTP and HTTP servers) run as daemon processes. Note, do not make the mistake that a daemon == server.

The Proc::Daemon::Init function does the following:

  1. Forks a child and exits the parent process.

  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.

  6. Closes all open file descriptors.

You will notice that no logging facility, or other functionality is performed. Proc::Daemon::Init just performs the main steps to initialize a program as daemon. Since other funtionality can vary depending on the nature of the program, Proc::Daemon leaves the implementation of other desired functionality to the caller, or other module/library (like Sys::Syslog).

There is no meaningful return value Proc::Daemon::Init. If an error occurs in Init so it cannot perform the above steps, than it croaks with an error message. One can prevent program termination by using eval.

OTHER FUNCTIONS

Proc::Daemon also defines some other functions. These functions can be imported into the callers name space if the function names are specified during the use declaration:

Fork

Fork is like the built-in fork, but will try to fork if at all possible, retrying if necessary. If not possible, Fork will croak.

OpenMax

OpenMax returns the maximum file descriptor number. If undetermined, 64 will be returned.

NOTES

  • Proc::Daemon::init is still available for backwards capatibilty. However, it will not perform the double fork, and will return the session ID.

AUTHOR

Earl Hood, earl@earlhood.com

http://www.earlhood.com/

CREDITS

Implementation of Proc::Daemon derived from the following sources:

  • Advanced Programming in the UNIX Environment, by W. Richard Stevens. Addison-Wesley, Copyright 1992.

  • UNIX Network Progamming, Vol 1, by W. Richard Stevens. Prentice-Hall PTR, Copyright 1998.

DEPENDENCIES

Carp, POSIX.

SEE ALSO

POSIX, Sys::Syslog