How to Create Daemon on Linux

Daemons are processes that do not run directly under the control of the user, but run in the background. Usually, they start at system startup and run continuously until the system shuts down.

The only difference between these processes and the normal process is that they do not send notifications to the console or screen in any way. Here is how you can create daemon on Linux machine.

A brief introduction to how daemons are created

Many daemons run on the system and some examples of familiar daemons are as follows:

  1. crond: Causes commands to run at the specified time
  2. sshd: Allows login to the system from remote machines
  3. httpd: Provide web pages
  4. nfsd: Allows file sharing over the network

In addition, daemon processes are often named ending with the letter d, although this is optional.

To have the process run as a daemon, follow this procedure:

  1. Initial operations, such as reading configuration files or obtaining necessary system resources, must be performed before the process becomes a daemon. In this way, the system can report received errors to the user and the process will be terminated with a suitable error code.
  2. A background process is created with init as its parent process. For this purpose, a child process is detached from the first init process, and then the above process is terminated.
  3. A new session will be opened by calling the setid function and the process will be disconnected from the terminal.
  4. All open file descriptors inherited from the parent process are closed.
  5. Standard input, output, and error messages are redirected to /dev/null.
  6. The working directory of the process must be changed.

What is session Daemon?

After logging into the system through the terminal, the user can run many applications through the shell program. These processes will close when the user exits the system. The operating system groups these processes into session and process groups.

Each session consists of groups of processes. This situation can be described as follows:

How to Create Daemon on Linux Picture 1How to Create Daemon on Linux Picture 1

The terminal where processes receive their input and send their output is called the controlling terminal. Each controlling terminal is associated with only one session at a time.

A session and process groups that have an identifier (ID). These identifiers are the process identifiers (PIDs) of the session and process leader group. A child process shares the same pool as its parent process. When multiple processes are communicating with the pipe mechanism, the first process becomes the process group leader.

Create Daemon Process on Linux

Here you will see how you can create a daemon function. For this purpose, you will create a function named _daemon. You can start by naming the application code that will run as a daemon test.c and the code you will create the daemon function with daemon.c.

//test.c #include int _daemon(int, int); int main() { getchar(); _daemon(0, 0); getchar(); return 0; }
//daemon.c #include #include #include #include #include #include #include #include int _daemon(int nochdir, int noclose) { pid_t pid; pid = fork(); // Fork off the parent process if (pid < 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } return 0; } 

To create a daemon, you need a background process whose parent is init. In the above code, _daemon creates a child process and then terminates the parent process. In this case, your new process will be a child process of init and will continue to run in the background.

Now compile the application with the following command and check the status of the process before and after _deamon is called:

gcc -o test test.c daemon.c

Run the application and switch to another terminal without pressing any other key:

./kiểm TRA

You can see that the values ​​related to your progress are as follows. Here you will have to use the ps command to get information regarding the process. In this case, the _daemon function has not been called yet.

ps -C test -o "pid ppid pgid sid tty stat command" # Output PID PPID PGID SID TT STAT COMMAND 10296 5119 10296 5117 pts/2 S+ ./test

When you look at the STAT field, you see that your process is running but is waiting for an unscheduled event to happen that will cause it to run in the foreground.

Abbreviation Meaning
S Sleeping waiting for an event to happen
BILLION Application has stopped
S Session leader
+ The application is running in the foreground

You can see that the main process of the application is the shell as expected.

ps -jp 5119 # Output PID PGID SID TTY TIME CMD 5119 5119 5117 pts/2 00:00:02 zsh

Now go back to the terminal where you are running your application and press Enter to call the _daemon function. Then, review the progress information on another terminal.

ps -C test -o "pid ppid pgid sid tty stat command" # Output PID PPID PGID SID TT STAT COMMAND 22504 1 22481 5117 pts/2 S ./test

First of all, you can tell that the new child process is running in the background because you don't see the + character in the STAT field. Now let's check which is the parent of the process using the following command:

ps -jp 1​​​​# Output PID PGID SID TTY TIME CMD 1 1 1 ? 00:00:01 systemd

Now you can see that the parent process for your process is systemd. It was mentioned above that for the next step, a new session will open and the process should be disconnected from the controlling terminal. For this you use the setid function. Add this call to your _daemon function.

The code to add is as follows:

if (setsid() == -1) return -1;

Now that you have checked the state before _daemon is called, you can now remove the first getchar function in the test.c code.

//test.c #include int _daemon(int, int); int main() { _daemon(0, 0); getchar(); return 0; }

After compiling and running the application again, go to the terminal where you did your evaluations. The new state of the process is as follows:

ps -C test -o "pid ppid pgid sid tty stat command"​​​​​# Output PID PPID PGID SID TT STAT COMMAND 25494 1 25494 25494 ? Ss ./test

Symbols ? in the TT field indicates that your process is no longer connected to the terminal. Note that the PID, PGID and SID values ​​in your process are the same. Your process is now a session leader.

In the next step, change the working directory to the root directory according to the value of the argument you passed. You can add the following code to the _daemon function for this:

if (!nochdir) { if (chdir("/") == -1) return -1; }

Now, according to the passed argument, all file descriptors can be closed. Add the following code to the _daemon function:

#define NR_OPEN 1024 if (!noclose) { for (i = 0; i < NR_OPEN; i++) close(i); open("/dev/null", O_RDWR); dup(0); dup(0); }

After all file descriptors are closed, new files opened by the daemon will be displayed with descriptors 0, 1 and 2 respectively. In this case, for example, the printf commands in the code will be redirected to the second file opened. To avoid this, the first 3 identifiers point to the device /dev/null.

In this case, the final state of the _daemon function will look like this:

#include #include #include #include #include #include #include #include #include int _daemon(void) { // PID: Process ID // SID: Session ID pid_t pid, sid; pid = fork(); // Fork off the parent process if (pid < 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } // Create a SID for child sid = setsid(); if (sid < 0) { // FAIL exit(EXIT_FAILURE); } if ((chdir("/")) < 0) { // FAIL exit(EXIT_FAILURE); } close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); while (1) { // Some Tasks sleep(30); } exit(EXIT_SUCCESS); } 

Here is an example code that runs the sshd application as a daemon:

. if (!(debug_flag || inetd_flag || no_daemon_flag)) { int fd; if (daemon(0, 0) < 0) fatal("daemon() failed: %.200s", strerror(errno)); /* Disconnect from the controlling tty. */ fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); if (fd >= 0) { (void) ioctl(fd, TIOCNOTTY, NULL); close(fd); } } .

Daemons are programs that perform various actions in a predefined manner set up in response to certain events. They run silently on your Linux machine. They are not under direct user control, and each background service has its own daemon.

It is important to master daemons to understand the kernel structure of the Linux operating system and understand the workings of different system architectures.

4 ★ | 2 Vote