daemon
Run a program as a background process
TLDR
Run a command as a daemon
Run a command as a daemon which will restart if the command crashes
Run a command as a daemon which will restart if it crashes, with two attempts every 10 seconds
Run a command as a daemon, writing logs to a specific file
Kill a daemon (SIGTERM)
List daemons
SYNOPSIS
daemonize [options] <program> [arguments...]
PARAMETERS
-a, --chdir <directory>
Change the current working directory of the daemon to <directory>.
-c, --close_fd
Close all open file descriptors. By default, stdin, stdout, and stderr are handled.
-e, --err <file>
Redirect the standard error (stderr) of the daemon to <file>.
-l, --logfile <file>
Redirect both standard output (stdout) and standard error (stderr) to <file>.
-o, --out <file>
Redirect the standard output (stdout) of the daemon to <file>.
-p, --pidfile <file>
Write the Process ID (PID) of the daemon to the specified <file>, useful for process management.
-u, --user <user>
Change the effective user ID to <user> after daemonizing for security purposes.
-g, --group <group>
Change the effective group ID to <group> after daemonizing for security purposes.
-v, --verbose
Enable verbose output, printing information about the daemonization process to stderr.
-h, --help
Display a help message and exit.
-V, --version
Display version information and exit.
--
Indicate the end of options; all subsequent arguments are treated as the command to run.
DESCRIPTION
daemonize is a lightweight yet powerful utility designed to transform any standard command or script into a proper Unix daemon process. Unlike simply backgrounding a process with `&`, daemonize handles the intricate steps required for robust daemonization: it forks the process into the background, detaches it from the controlling terminal, thereby making it immune to hangup signals (SIGHUP) when the launching shell exits. Additionally, it redirects standard input, output, and error streams, typically to /dev/null or specified log files, preventing unintended terminal output or input dependencies. It also changes the current working directory to / by default (or a user-specified path) to avoid issues with unmounting filesystems. This command is invaluable for developers and system administrators who need to run applications or scripts continuously in the background as services without them being tied to an interactive session, ensuring stability and independence.
CAVEATS
Not a universally standard Linux command; it often requires explicit installation (e.g., apt install daemonize on Debian/Ubuntu, yum install daemonize on CentOS/RHEL). Its availability may vary across distributions.
daemonize itself does not provide service management capabilities like automatic restarts on failure, dependency management, or integration with system-wide service hierarchies. For these features, it should be used in conjunction with a service manager like systemd (e.g., by calling daemonize from within a .service unit file) or SysVinit scripts.
While daemonize handles the initial setup, the launched program must still be designed to run indefinitely and handle its own lifecycle (e.g., graceful shutdown on signals) if required.
If the program being daemonized itself performs daemonization steps (e.g., by calling daemon(3) or doing its own fork()/setsid()), using daemonize on it might lead to unexpected behavior or redundant operations.
CORE DAEMONIZATION STEPS
A well-behaved Unix daemon typically performs a series of critical steps upon startup to properly detach from its environment: 1) Forking: The parent process forks, and the child continues. 2) Parent Exit: The original parent process exits, detaching the child from the controlling terminal. 3) Session Leader: The child calls setsid() to create a new session and become the session leader, ensuring it doesn't acquire a controlling terminal. 4) Second Fork (optional but common): The session leader forks again, and the grandchild exits. This prevents the session leader from reacquiring a controlling terminal. 5) Change Directory: The working directory is changed to / or a suitable location to prevent issues with unmounting filesystems. 6) Close File Descriptors: All open file descriptors (especially stdin, stdout, stderr) are closed to prevent resource leaks and terminal interaction. 7) Redirect Standard I/O: stdin, stdout, and stderr are redirected to /dev/null or log files to prevent unexpected output or input requests. The daemonize command automates most of these crucial steps.
USAGE WITH SERVICE MANAGERS
While daemonize effectively launches a process as a daemon, it does not offer full lifecycle management (e.g., start, stop, restart, status, dependencies). For robust service management, it is typically integrated with system-wide service managers like systemd or traditional SysVinit. For systemd, you would define a `.service` unit file where the `ExecStart` directive invokes daemonize with your command. This allows systemd to supervise the daemon, handle its logging, manage its dependencies, and ensure it restarts automatically if it crashes, providing a complete and resilient service solution.
HISTORY
The concept of a "daemon" process has existed since the earliest days of Unix, referring to background processes that run continuously without direct user interaction. Early daemonization involved complex manual steps within program code or shell scripts. The daemonize utility emerged in the early 2000s as a simpler, standardized way to wrap arbitrary executables into proper daemons, abstracting away the error-prone boilerplate like double-forking, detaching from the controlling terminal, and managing file descriptors. It filled a gap for users who needed to run simple scripts or programs as background services without writing full daemon-aware applications or complex init.d scripts. Its development focused on providing a robust, standalone tool for a common system administration task.
SEE ALSO
nohup(1), disown(1), systemd.service(5), start-stop-daemon(8), init(8)