daemonize
Run a program as a daemon
TLDR
Run a command as a daemon
Write the PID to the specified file
Use a lock file to ensure that only one instance runs at a time
Use the specified user account
SYNOPSIS
daemonize [options]
PARAMETERS
-a, --successful-start-wait
Wait for the daemon process to signal successful startup via exit code, up to the specified number of seconds.
-c, --chdir
Change the current working directory to the specified
-e, --errlog
Redirect the standard error stream (stderr) of the daemon to the specified
-g, --group
Change to the specified group ID (by name or GID) before executing the command, after changing user ID if specified.
-l, --pidfile
Write the daemon's process ID (PID) to the specified
-o, --outfile
Redirect the standard output stream (stdout) of the daemon to the specified
-p, --pid-file-perms
Set permissions (e.g., 0644) for the PID file specified with -l.
-u, --user
Change to the specified user ID (by name or UID) before executing the command.
-v, --verbose
Enable verbose output, showing details about the daemonization steps.
-D, --no-close-stdio
Do not close standard input, output, and error streams. Use with caution as this can prevent true daemonization.
-k, --kill-after-failure
If the daemonized command exits immediately with a non-zero status, kill the parent process of daemonize.
--
Marks the end of daemonize options and the beginning of the command to be executed by the daemon. This is useful if the command itself has options that might be misinterpreted by daemonize.
DESCRIPTION
daemonize is a utility designed to transform a regular command or script into a well-behaved Unix daemon. A daemon is a background process that runs independently of a controlling terminal, typically performing system-wide tasks.
The process of daemonization involves several crucial steps: forking the process to detach it from the parent, creating a new session using setsid() to ensure it's not tied to any terminal, changing the current working directory to the root (/) to avoid keeping directories in use, and redirecting standard input, output, and error streams (usually to /dev/null) to prevent I/O issues.
While many robust applications are designed to daemonize themselves, daemonize is particularly useful for simple scripts, legacy programs, or applications that lack built-in daemonization capabilities. It automates these complex steps, ensuring that the target command runs reliably in the background, is resilient to terminal disconnections, and properly manages its process ID. It's an invaluable tool for system administrators and developers needing to run services without direct terminal interaction.
CAVEATS
daemonize is designed to run non-daemonizing programs as daemons. If the wrapped command attempts to daemonize itself (e.g., by calling fork() and setsid() internally), it can lead to unexpected behavior, including double-forking or incorrect PID file management.
Error logging is crucial when using daemonize, as the daemon's standard output and error streams are typically redirected to files or /dev/null. Without proper logging via the -o and -e options, debugging issues with the background process can be challenging.
It handles basic daemonization but does not provide advanced service management features found in modern init systems like systemd (e.g., dependency management, automatic restarts, resource limits, cgroups). For robust production services, systemd unit files are often preferred.
HOW IT WORKS INTERNALLY
When daemonize executes, it performs a sequence of operations to achieve proper daemonization:
1. It forks the parent process, and the parent exits, allowing the shell to return control immediately.
2. The child process then calls setsid() to become the session leader of a new session and disconnect from any controlling terminal.
3. It changes its current working directory to the root directory (/) to prevent keeping any mount points or directories busy.
4. It closes standard file descriptors (stdin, stdout, stderr) and reopens them, typically redirecting them to /dev/null, or to specified log files using -o and -e options.
5. Finally, it executes the target command, which then runs as an independent daemon.
USE CASES AND ADVANTAGES
daemonize is ideal for running scripts that need to persist after a user logs out, such as custom cron-like jobs, background listeners, or simple web servers written in scripting languages. Its primary advantage is its simplicity and ability to convert almost any command into a proper daemon without requiring the command itself to handle daemonization logic, thus reducing development effort and ensuring adherence to Unix daemon conventions.
HISTORY
The concept of a daemon has existed since the early days of Unix. Programmers traditionally implemented daemonization steps directly within their applications (fork, setsid, chdir /, close stdio). The daemonize utility emerged as a convenient wrapper to encapsulate these common steps, making it easier to run existing scripts or simple programs as proper daemons without modifying their source code. It is not part of core GNU utilities and is typically distributed as a separate package, reflecting its role as a specialized tool for this specific purpose rather than a fundamental shell command. Its development fills a gap for cases where a full-fledged init system service definition might be overkill, or the application doesn't natively daemonize.