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 [-a] [-c DIR] [-e FILE] [-o FILE] [-l FILE] [-p FILE] [-u USER] [-n LEVEL] [-r] program [args...]
PARAMETERS
-a, --append
Append to output files instead of overwriting.
-c DIR, --chdir=DIR
Change working directory to DIR before running program.
-e FILE, --stderr=FILE
Redirect stderr to FILE (default: /dev/null).
-o FILE, --stdout=FILE
Redirect stdout to FILE (default: /dev/null).
-l FILE, --lockfile=FILE
Create lockfile at FILE using hostname.pid pattern.
-p FILE, --pidfile=FILE
Write daemon PID to FILE.
-u USER, --user=USER
Switch to user USER before exec (requires privileges).
-n LEVEL, --nicelevel=LEVEL
Set process nice level to LEVEL.
-r, --inetd
Run as if invoked by inetd (no forking).
-E VAR=VAL, --env=VAR=VAL
Set environment variable VAR to VAL.
DESCRIPTION
Daemonize is a lightweight utility that transforms any command or script into a proper Unix daemon process. Daemons run detached from the controlling terminal in the background, handling system services without user interaction.
It automates essential daemonization steps: double-fork to detach from the parent process and terminal, call setsid() for a new session, change working directory to /, reset umask to 0, close all open file descriptors (or redirect them), and optionally create PID/lock files, adjust nice levels, or switch users.
This eliminates the need for complex C code or error-prone shell scripts implementing these via fork(), setsid(), etc. Ideal for service wrappers, cron jobs needing persistence, or quick daemon prototypes. Install via package managers (e.g., apt install daemonize). Supports logging to files for monitoring.
CAVEATS
Not installed by default; requires package installation. PID/lock files not auto-removed on exit. -u needs root privileges. Avoid for modern systems preferring systemd units.
EXAMPLE
daemonize -o /var/log/myservice.log -p /var/run/myservice.pid -u nobody ./myscript.sh
nohup daemonize -a -c /opt/app /usr/bin/python app.py &
EXIT STATUS
0 on success; 1 if program exec fails; 2+ for daemonization errors (check logs).
HISTORY
Developed by Niall Doherty around 2003-2004 as a simple C tool for daemonization. Maintained as open-source (GPL); version 1.0.x common in distros since mid-2000s. Precedes widespread systemd adoption.
SEE ALSO
nohup(1), setsid(1), start-stop-daemon(8), daemon(1)


