13

http://linuxg.net/how-to-transform-a-process-into-a-daemon-in-linux-unix/ gives an example of daemonizing a process in bash:

$ nohup firefox& &> /dev/null

If I am corrrect, the command is the same as "nohup and background a process". But isn't a daemon more than a nohupped and background process?

What steps are missing here to daemonize a process?

For example, isn't changing the parent process necessary when daemonizing a process? If yes, how do you do that in bash? I am still trying to understand a related reply https://unix.stackexchange.com/a/177361/674.

What other steps and conditions?

See my related question https://stackoverflow.com/q/35705451/156458

3
  • 1
    depends on your definition of daemons. If you merely mean running in the background detached from a terminal then yes you are running firefox as a daemon. "standard" daemons, however, typically are not run by users, have an init script and logging, and typically some sort of security, often apparmor or selinux depending on if you are running Ubuntu or Fedora (or similar). See linfo.org/daemon.html .
    – Panther
    Feb 29, 2016 at 18:02
  • 1
    Have a look at start-stop-daemon in Debian ; I will leave here a related thread from stack overflow stackoverflow.com/questions/16139940/… which is more interesting than the raw man page Feb 29, 2016 at 18:39
  • The link redirects to linux games
    – shlomoa
    Mar 20 at 8:05

2 Answers 2

18

From the Wikipedia article on daemon:

In a Unix environment, the parent process of a daemon is often, but not always, the init process. A daemon is usually either created by a process forking a child process and then immediately exiting, thus causing init to adopt the child process, or by the init process directly launching the daemon. In addition, a daemon launched by forking and exiting typically must perform other operations, such as dissociating the process from any controlling terminal (tty). Such procedures are often implemented in various convenience routines such as daemon(3) in Unix.

Read the manpage of the daemon function.

Running a background command from a shell that immediately exits results in the process's PPID becoming 1. Easy to test:

# bash -c 'nohup sleep 10000 &>/dev/null & jobs -p %1'
1936
# ps -p 1936
      PID    PPID    PGID     WINPID   TTY         UID    STIME COMMAND
     1936       1    9104       9552  cons0       1009 17:28:12 /usr/bin/sleep

As you can see, the process is owned by PID 1, but still associated with a TTY. If I log out from this login shell, then log in again, and do ps again, the TTY becomes ?.

Read here why it's important to detach from TTY.

Using setsid (part of util-linux):

# bash -c 'cd /; setsid sleep 10000 </dev/null &>/dev/null & jobs -p %1'
9864
# ps -p 9864
      PID    PPID    PGID     WINPID   TTY         UID    STIME COMMAND
     9864       1    9864       6632  ?           1009 17:40:35 /usr/bin/sleep

I think you don't even have to redirect stdin, stdout and stderr.

4
  • 4
    Take a look at daemonize. Besides a nice tool, it has pretty good explanations on what a daemon is. Apr 21, 2016 at 20:50
  • I'm just curious if you know a way to dissociate the process from the tty without logging out from the shell from which the process was spawned?
    – StoneThrow
    Aug 23, 2017 at 22:22
  • 1
    @StoneThrow "without logging out from the shell from which the process was spawned" If you use the bash -c wrapper, there is no TTY associated with the process. It really is just like he demonstrated it. Dec 3, 2018 at 5:12
  • @StoneThrow but doing this test echo "outer tty: $(tty)"; ls -la $(dirname $(tty)); bash -c 'echo "inner tty: $(tty)"; ls -la $(dirname $(tty));' will show you that it is the exact same TTY for both, but the demonstrated behavior of getting a TTY of ? still happens even though you're never "logging out from the shell" nor closing the TTY. Dec 3, 2018 at 5:19
-1

A deamon, by its name is nothing more than a program that runs until 1. the system is shutdown; 2. it is requested to stop. Other than that, it has no magic meaning.

Under the circumstances, running a bash script in the background with nohup, can classify it as a daemon process.

What are you expecting to find and not finding ? If you are having any problems, please state them with sample code and sample data segments to ask for further help. Your question, as it stands right now is too broad/general.

2
  • Thanks. I think my post has all you asked for in your last paragraph.
    – Tim
    Feb 29, 2016 at 18:04
  • 4
    Umm this isn’t true. A daemon has no controlling Terminal, has no stdout or stderr, and some other things. Read the first stanza of software.clapper.org/daemonize Nov 10, 2017 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .