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 -1 and sets errno to any of the errors specified for the fork(2) and setsid(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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms