Here’s how you can create a daemon on a Linux machine.

A Brief Introduction to How Daemons Are Created

A lot of daemons run on the system and some familiar daemon examples are as follows:

crond: Makes commands run at the specified time sshd: Allows login to the system from remote machines httpd: Serves web pages nfsd: Allows file sharing over the network

Also, daemon processes are usually named to end with the letter d, although it’s not mandatory.

For a process to run as a daemon, the following path is followed:

Initial operations, such as reading configuration files or obtaining necessary system resources, must be performed before the process becomes a daemon. This way, the system can report the received errors to the user and the process will be terminated with an appropriate error code. A background running process is created with init as its parent process. For this purpose, a sub-process is forked from the init process first, and then the upper process is terminated with exit. A new session should open by calling the setsid function, and the process should be disconnected from the terminal. All open file descriptors inherited from the parent process are closed. Standard input, output, and error messages are redirected to /dev/null. The working directory of the process must change.

What Are Daemon Sessions?

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

Each session consists of process groups. You can describe this situation as follows:

The terminal where the processes receive their inputs and send their outputs is called the controlling terminal. A controlling terminal is associated with only one session at a time.

A session and the process groups in it have identification (ID) numbers; these identification numbers are the process identification numbers (PID) of the session and process group leaders. A child process shares the same group as its parent process. When multiple processes are communicating with the pipe mechanism, the first process becomes the process group leader.

Creating a 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 as test.c, and the code that you will create the daemon function as daemon.c.

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

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

Run the application and switch to a different terminal without pressing any other keys:

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

When you look at the STAT field, you see that your process is running but waiting for an off-schedule event to occur which will cause it to run in the foreground.

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

Now return to the terminal where you are running your application and press Enter to invoke the _daemon function. Then look at the process information on the other terminal again.

First of all, you can say that the new subprocess is running in the background since you do not see the + character in the STAT field. Now examine who is the parent process of the process using the following command:

You can now see that the parent process of your process is the systemd process. It is mentioned above that for the next step, a new session should open and the process should be disconnected from the control terminal. For this, you use the setsid function. Add this call to your _daemon function.

The piece of code to add is as follows:

Now that you’ve inspected the state before _daemon called, you can now remove the first getchar function in the test.c code.

After compiling and running the application again, go to the terminal where you made your reviews. The new status of your process is as follows:

The ? sign in the TT field indicates that your process is no longer connected to a terminal. Notice that the PID, PGID, and SID values of 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 snippet to the _daemon function for this:

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

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

In this case, the final state of the _daemon function will be as follows:

Here’s an example of a code snippet that runs the sshd application as a daemon:

Daemons Are Important for Linux System Programming

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

It is important to master daemons to learn the kernel structure of the Linux operating system and to understand the working of various system architectures.