Prefer systemd over self-daemonizing; if you must daemonize, fork twice, chdir, reset umask, redirect stdio.
Modern daemon rules
Prefer systemd
Type=simplelets systemd manage the process. No daemonizing code needed. The process just runs in the foreground.Fork twice if you must daemonize
fork→ parent exits →setsid→forkagain → grandparent exits. Child has no TTY, is a session leader.Chdir to /
A daemon should not keep the cwd pinned to a filesystem; that prevents unmounting.
Reset umask
umask(0)so the daemon's file creation is not inherited from the shell.Redirect stdio
freopen('/dev/null', 'r', stdin); freopen('/dev/null', 'w', stdout); freopen('/dev/null', 'w', stderr);Log to syslog or journald
Not to stdout. Logs need to survive the daemon's restart.