11 ms·
Understanding Daemons
- pmiller2 6y ago> Simply put, a daemon (pronounced dee-mon) is an intentionally orphaned background process. Additionally, daemons are detached from the terminal in which they’re spawned, operate without user interaction, and are descendants of the system’s init process. A memorable way to think about this is that a daemon is a child process whose parent has literally disowned it, forked off, and died.
- JdeBP 6y ago... except that that's most definitely not the defining characteristic, and the defining characteristic is rather that the process isn't part of a login session, which ironically on modern operating systems with all of the one-way trapdoors that now exist one cannot reach by forking. * https://unix.stackexchange.com/a/606902/5132 https://unix.stackexchange.com/a/606902/5132 Thinking that forking makes a dæmon is much the same error as thinking that su drops privileges, it conflates what happend to be a mechanism from years ago with the actual purpose, totally missing that things have changed since (long since, decades ago in fact).
- paledot 6y agoWelp, I've had a lot of emotions about Apache httpd over the years, but this is the first time over felt sorry for it. Poor lil' thing.
- timw4mail 6y agoGoing by the spelling, isn't it pronouced day-mon?
- SmooL 6y agoI've also been pronouncing it like this and so far I've never been called out, maybe I just have nice friends?
- macintux 6y agoI have to admit to pronouncing it consistently “daymonization” and sometimes but not always “demon”.
- aryamansharda 6y agoI was a bit conflicted on the pronunciation, but after verifying with a few sources it seems like it's pronounced just like "demon" https://en.wikipedia.org/wiki/Daemon_(computing)#:~:text=The%20word%20daemon%20is%20an,DAY%2Dm%C9%99n%20for%20some%20speakers https://en.wikipedia.org/wiki/Daemon_(computing)#:~:text=The....
- timw4mail 6y agoBut the first sentence has both pronunciations...so...
- jpxw 6y agoNo, in the same way that paediatrician isn’t pronounced paydiatrician
- pmiller2 6y agoIt's also not spelled "paediatrician" in American English, hence the confusion.
- rgoulter 6y agoTo be fair, it's not obvious how to pronounce a way it's spelt. e.g. "record" and "record" are pronounced differently, but have the same spelling. The poem "Dearest creature in creation" is a fun illustration of the differences between spelling and pronunciation.
- macintux 6y agoOf all the great content in APUE, I think W. Richard Stevens’ (RIP) coverage of daemonization is the only piece I’ve ever used. Fortunately I recently replaced my lost 1st edition with a 2nd edition, and the coverage looks similar. From what I can see there are two details left out of this article: * Why changing the working directory is important (so the daemon doesn’t accidentally prevent a file system from being unmounted in the future). * WRS emphasized logging. Some of the details are different (umask as the first function instead of the last), but the highlights are comparable.
- zokier 6y agoWith systemd (and afaik other service managers) you don't need/should to do all that daemonization dance. The application can run in the foreground, keep any fds open, don't worry about tty etc because the service manager will take care of them for you. As a cherry on top, anything you write on stdout gets automatically forwarded to journald or wherever. further reading https://www.man7.org/linux/man-pages/man7/daemon.7.html https://www.man7.org/linux/man-pages/man7/daemon.7.html
- gilgad13 6y agoMy understanding was that the double fork dance was actively discouraged for any semi-modern system. The man page you link says as much in the "new-style" section, as well as https://jdebp.eu/FGA/unix-daemon-design-mistakes-to-avoid.html https://jdebp.eu/FGA/unix-daemon-design-mistakes-to-avoid.ht.... Just write your server as a straightforward process and let the service manager handle this stuff for you. You'll just be confusing it if you don't.
- JdeBP 6y agoIt is rather sad to see something that was out of date in the 1990s still being circulated today. Even the part about process #1 inheriting orphaned processes is 8 or so years out of date. * https://unix.stackexchange.com/a/177361/5132 https://unix.stackexchange.com/a/177361/5132
- vimax 6y agoThe more I use systemd the more I love it. It simplifies so much, and adds all the little features I see so many projects using custom implementations for. The people who keep complaining about it seem more and more like children who just refuse to learn something new and better because they've already stumbled through all the holes it fills.
- efiecho 6y ago> The people who keep complaining about it seem more and more like children who just refuse to learn something new and better because they've already stumbled through all the holes it fills. Systemd apologists always seems to think that anyone not completely in love with systemd, is just refusing to learn something new. Please stop with the stupidity, people have real reasons to hate that shit. I had no problem with switching to WireGuard, after learning about it, because this is so much better in both use and design than OpenVPN that I have used for 20 years. I had no problem with learning about systemd either, but that is just badly designed software to me.
- bfuclusion 6y agoSo how does this work in windows? Edit, as for some reason it's getting downvoted: In windows, do you have to do the double fork etc? Do you have to close FDs etc?
- meddlepal 6y agoWindows has a services abstraction though I'm admittedly not very familiar with it. I suspect there's not a lot of Win-dev folks on HN.
- bfuclusion 6y agoYeah, I've done some python daemons in win, and registered with the service control manager, but I was wondering what the "magic" was there.
- Joker_vD 6y agoThe Service Control Manager (SCM) has a list of settings for registered services: the parameters of a service are its name, executable path, arguments, environment variables, user to run it under, other services it depends on, restarting options, etc. When the system is started, the SCM starts those services. When the system is being shut down, the SCM stops them. How is a service started? The SCM launches the specified executable, in the way it's specified in the service's settings and... that's pretty much it. However, the service is supposed to support a certain protocol of interaction with SCM. First of all, the service must quickly (within half a minute from the start, IIRC) call the WinAPI function StartServiceCtrlDispatcher() on its main thread. That's basically a pre-written main(): it returns only after the service has been stopped. It takes several function pointers: to the ServiceMain where the service proper is supposed to be, and various event handlers (pause/resume/stop requests, system is shutting down, etc). It creates a separate thread to run the ServiceMain on, and then basically loops processing requests/events from the SCM (event handlers are run on the main thread). That's basically it. Nothing prevents one to write an application that can run both as a service and stand-alone: put your logic in ServiceMain, call StartServiceCtrlDispatcher with it, if it returns ERROR_FAILED_SERVICE_CONTROLLER_CONNECT (that means you're not being launched by the SCM), call ServiceMain by yourself. Ctrl-C handler and "stop the service" handler are easily unified because on Windows, Ctrl-C handler always runs on a different thread. You just need make sure that your ServiceMain function can be safely signaled that it needs to stop from a different thread, but that's what thread-communication primitives are for.
- nemetroid 6y ago> Conventionally, daemon process names end with the letter “d”. If you’re interested in seeing all daemons installed on your Linux machine, use this command: > service --status-all $ service --status-all bash: service: command not found
- hyper_reality 6y agoPerhaps you're trying to make a point, but the command should work with /usr/sbin/service or prefaced by sudo. If you run `whereis service` you can see that the binary is located in /usr/sbin (at least on Debian/Ubuntu systems) which is typically placed in the PATH of root but not normal users. However --status-all doesn't require elevated permissions so the command works if you provide the full path.
- xeeeeeeeeeeenu 6y agoSome distributions, for example Slackware, don't have it at all.
- nemetroid 6y agoA bit of both. My computer does not have the "service" command at all. While it's true that many systems still do, it's typically there as a legacy compatibility layer, to translate SysV style commands to systemd. So while the command might work for many readers, I don't think it's a good idea to send beginners that way, and its inclusion (without any footnotes or other qualifications) suggests that the author's knowledge is out of date.
- emmanueloga_ 6y agoI was hoping this would be an article for beginners to understand daemons, as in the Ancient Greek daimon (δαίμων: "god", "godlike", "power", "fate") :-) Growing up on a (quasi fundamentalist) christian household, daemons were for a long while a very real threat, and my mom would not allow me to watch horror movies, "receive things from strangers", an other procedures to protect us from "inviting the daemons home". My rational mind leaves no room for such creatures these days, but even so under the right circumstances, sometimes my lizard brain begins to think maybe the Bible (and my mom for that matter) was right :-). Hard to erase some emotive memories from childhood I guess. A while ago I read this book [1] which is pretty cool, covers the mind mechanisms behind things like the placebo effect, "daemonic possession", alien abductions, mass hysteria, out of body experiences, etc... 1: https://www.suggestibleyou.com/ https://www.suggestibleyou.com/
- Skunkleton 6y agoUsually I see δαίμων latinized as "demon" not "daemon". You got me looking and I found an interesting discussion on these words over on stack exchange [1]. TL;DR, "daemon" sometimes means "demon", but without the negative connotations, other times it is just an alternative spelling. [1] https://english.stackexchange.com/questions/39266/what-is-the-difference-between-daemon-and-demon-in-a-religious-context https://english.stackexchange.com/questions/39266/what-is-th...
- emmanueloga_ 6y agoOh! The good daemons! "benevolent or benign nature spirits...". Not in the Judeo-Christian tradition, for sure :-) Also got me curious about the C.S. ethymology of the word. According to the venerable Jargon File (which has entries for both "daemon" and "demon"), it is named in honor of one of Maxwell's thought experiments! [1] 1: http://www.catb.org/jargon/html/D/daemon.html http://www.catb.org/jargon/html/D/daemon.html
- Skunkleton 6y agoAhh, neat. Thanks for the link.
- totemandtoken 6y agoVery timely to see this. I was trying to daemonize some celery workers/celery tasks and I wasn't sure how to go about it. Glad this came up
- tus88 6y agoThey have a double fork - forked tongue and forked tail, and often cannot be killed through normal means, re-spawning with the aid of an even greater evil known as SystemD.
- wruza 6y agoA good practice for a classical daemon and any program in general after closing all descriptors is to reopen first three (0,1,2) to /dev/null. After that, any forgotten or unexpected diagnostics (rare printf in third party library) would not corrupt your open files.
- JdeBP 6y agoNo, that's a bad practice, because since the late 1990s service management subsystems have been as the norm setting up standard output and standard error so that they are connected to loggers. daemontools did it, once it gained svscan; and so did the other toolsets in the daemontools family such as runit, perp, s6, and others. systemd also does it, but is very much a latecomer here. Furthermore, this bad practice lands one in hot water with library functions, including ironically syslog client libraries, that open and retain file descriptors internally for their own purposes. Yet another case of this was just on Hacker News this week, in fact, at https://news.ycombinator.com/item?id=24329540 https://news.ycombinator.com/item?id=24329540 .
- wruza 6y agoThat's still a good practice, if you close descriptors. Such criticism and trouble comes from environments that are unable to follow the routine because reasons, e.g. they openlog() and construct other objects before daemonizing. Given how much can happen before python's main() to be called, it is obvious that daemonizing python-anything is a bad idea to start with. It only works for few controlled steps at int main(), after that you're screwed. All these pitfalls are discussed in literature, the real problem is that barely anyone bothers to read it and think for a second on their options. One of them being to use a proper service manager instead, like systemd.
- JdeBP 6y agoNo-one can follow the routine. "daemonization" is a fallacy.
- tsimionescu 6y agoThis function may do mostly the right thing in a process dedicated to it, but it is not safe to call as part of a more complex process - especially a multi-threaded one. For example, malloc() may be holding on to a mutex in the parent process (on another thread), so subsequent malloc() calls in the child process will block waiting for the mutex to be released. Closing the file handles may help if this was a kernel mutex, but it may not help if this was a user-space mutex. In general, fork() without exec() is only safe as the first thing done in a process. Also, it looks like there is a minor race condition in the implementation: since the original process exit()s immediately after the fork(), control will return to its parent, which may close the session - this could happen before the child had a chance to call setsid() and detach from the parent session.