daemon(3)
โจ daemon(3) โ the "go run in the background bestie" page โจ
okay so this is daemon(3) from the Library Functions Manual, and it lives in the Standard C library, aka libc, aka -lc. she's been around forever and she's lowkey iconic. let's get into it ๐
๐ธ NAME ๐ธ
daemon โ run in the background
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐ซถ
๐ SYNOPSIS ๐
#include <unistd.h>
int daemon(int nochdir, int noclose);
๐ฆ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ๐ฆ
daemon():
- Since glibc 2.21:
_DEFAULT_SOURCE
- In glibc 2.19 and 2.20:
_DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
- Up to and including glibc 2.19:
_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
๐ DESCRIPTION ๐
so the daemon() function is for programs that literally want to detach themselves from the controlling terminal and go run in the background as system daemons. main character energy, no audience, just vibes. ๐ง
if nochdir is zero, daemon() changes the process's current working directory to the root directory ("/"). otherwise (i.e. if nochdir isn't zero), the current working directory is left unchanged. your call, bestie.
if noclose is zero, daemon() redirects standard input, standard output, and standard error to /dev/null. otherwise, no changes are made to these file descriptors. it's giving "silence is golden" โจ
๐ฅ RETURN VALUE ๐ฅ
(this function forks, and if the fork(2) succeeds, the parent calls _exit(3), so that further errors are seen by the child only.) โ yes, that's a whole thing, keep reading.
- on success,
daemon()returns zero. slay. โ - if an error occurs,
daemon()returns-1and setserrnoto any of the errors specified for thefork(2)andsetsid(2). ๐ญ
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, see attributes(7).
daemon()โ Thread safety:MT-Safe
๐ VERSIONS ๐
a similar function appears on the BSDs. (sisters, not twins fr fr)
the glibc implementation can also return -1 when /dev/null exists but is not a character device with the expected major and minor numbers. in this case, errno need not be set. sneaky lil edge case, ngl. ๐
โญ STANDARDS โญ
None. (she's not standardised, she's just her. periodt.)
๐ HISTORY ๐
4.4BSD.
๐ BUGS ๐
okay bestie, this one's important, don't scroll past it ๐
the GNU C library implementation of this function was taken from BSD, and does not employ the double-fork technique (i.e., fork(2), setsid(2), fork(2)) that is necessary to ensure that the resulting daemon process is not a session leader. instead, the resulting daemon is a session leader.
on systems that follow System V semantics (e.g., Linux), this means that if the daemon opens a terminal that is not already a controlling terminal for another session, then that terminal will inadvertently become the controlling terminal for the daemon. ๐
not me warning you that your daemon might accidentally adopt a terminal it did not ask for. that's the bug, that's the vibe, be aware.
๐ธ SEE ALSO ๐ธ
and that's the whole page, bestie!! this was Linux man-pages 6.20, dated 2026-06-05, and the page we just read was daemon(3) ๐ซถ go forth and detach responsibly ๐โจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1993, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms