12

I hear a lot of talk about Daemons running on my Ubuntu computer - what are they?

15

In short, a Daemon is a background process.

Daemons can just be normal programs that run in the background, however most are created by starting a process, forking it and exiting the parent.

To fork a process means to create an exact copy of it. The parent of that process, if the real parent terminates right away, is now the init process at /sbin/init, which is the first thing started on every Unix-like operating system. Now, the process is termed a Daemon, it has no TTY associated with it.

Here's an example of a Daemon in Python:

import sys, os, time

pid = os.fork()

# there now exist two processes
if pid > 0: # If this is the parent,
    sys.exit(0) # quit.

# this is the background part:
time.sleep(5)
print "Hello, World!"

It's not yet one, strictly speaking. You'd also have to change the current working directory, redicted standard input and output to log-files and so on. You can read up on the gory details in this wikipedia article.

If you run the example, you'll notice, after two seconds it prints, even though the process you started on the command-line has terminated. The copy of this process is run 'by' init now.

  • 8
    to expand on this, if you're coming to Linux from Windows, you can think of daemons as the *nix equivalent of Windows' "services". – nathwill Feb 15 '11 at 22:32
  • 1
    @stefano-palazzo You said "...it has no TTY associated with it", but then "...you'll notice, after two seconds it prints...". I thought detaching the TTY meant you couldn't write to the terminal. – Jorge Bucaran Jun 23 '16 at 4:52
  • 1
    @JorgeBucaran Yes. The printing is a bit naughty. It can still have a handle to the standard output stream of the tty that started it. When you open a file (like /dev/stdout), you get a number (called the file descriptor), and that is all you need to write to it as long as the process that has opened it is still running (you can see opened files in /proc/*/fd). A well behaved daemon will detach itself completely from the tty by giving up the handles to stdout, stderr, and stdin. – Stefano Palazzo Jun 23 '16 at 5:08
  • 1
    @StefanoPalazzo I see. One can retain a reference to the parent's process std/out/err and still detach from it. If I can arbitrarily keep a reference to the TTY, then, what does it mean to detach a process? I found <superuser.com/questions/178587/…> useful, but still feel unclear. – Jorge Bucaran Jun 23 '16 at 6:09
  • 1
    @JorgeBucaran it means to overwrite the processes own stdio (e.g. by using dup2) with new ones you have created (like a log file). Here's a full example – Stefano Palazzo Jun 23 '16 at 6:17
1

In multitasking computer operating systems, a daemon is a computer program that runs as a background process, rather than being under the direct control of an interactive user. Traditionally, the process names of a daemon end with the letter d, for clarification that the process is, in fact, a daemon, and for differentiation between a daemon and a normal computer program. For example, syslogd is the daemon that implements the system logging facility, and sshd is a daemon that serves incoming SSH connections.

As you can see the Daemon's layer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.