Open In App

Daemon Processes

Last Updated : 22 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A daemon process is a background process that runs independently of any user control and performs specific tasks for the system. Daemons are usually started when the system starts, and they run until the system stops. A daemon process typically performs system services and is available at all times to more than one task or user. Daemon processes are started by the root user or root shell and can be stopped only by the root user. 

  • A daemon process runs in the background without direct user interaction.
  • It is often started automatically when the system boots.
  • Daemons usually detach from their parent processes and run independently.
  • example - the "qdaemon" process, "sendmail" daemon etc.

How Daemon Processes are created

A daemon process is created by detaching a process from its parent and associating it with the init process. This is done in several steps: first, the process calls fork() to create a child process. Then, the child process calls setsid() to create a new session and detach from any terminal. Afterward, it changes its working directory to root (/), resets file permissions, and redirects standard input, output, and error to /dev/null. Finally, it runs in the background to perform its tasks.

Below figure explains the creation of Daemon Process:

daemon


  • A daemon is usually created either by a process forking a child process and then immediately exiting, thus causing init to adopt the child process.
  • Daemon can also be created by the init process directly launching the daemon.

Example

let's see an example of Daemon Process:

C
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>

int daemon_initialise() {
    pid_t pid;

    // Step 1: Fork the parent process
    if ((pid = fork()) < 0) {
        return -1; // Fork failed
    } else if (pid != 0) {
        exit(0); // Parent process exits
    }

    // Step 2: Create a new session
    if (setsid() < 0) {
        return -1; // Failed to create a new session
    }

    // Step 3: Change working directory to root
    if (chdir("/") < 0) {
        return -1; // Failed to change directory
    }

    // Step 4: Reset file permissions
    umask(0);

    // Step 5: Close standard file descriptors
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    // Step 6: Redirect standard input, output, and error to /dev/null
    open("/dev/null", O_RDONLY); // Redirect standard input
    open("/dev/null", O_WRONLY); // Redirect standard output
    open("/dev/null", O_WRONLY); // Redirect standard error

    return 0; // Daemon initialized successfully
}

int main() {
    if (daemon_initialise() < 0) {
        // Failed to initialize daemon
        exit(EXIT_FAILURE);
    }

    // Daemon logic (e.g., perform tasks in the background)
    while (1) {
        // Example: Sleep for 10 seconds
        sleep(10);
    }

    return 0;
}


This code shows a proper initialization of a daemon process. It creates a daemon that detaches from the terminal, changes its working directory to root, sets default permissions, and redirects standard input, output, and error to /dev/null. The daemon then performs its tasks in the background.

Handling Daemon Processes

Daemon processes are background processes that perform system tasks independently of user interaction. Proper handling of daemon processes is essential for ensuring system stability and functionality. Here are common ways to handle daemon processes:

  1. Starting Daemons:
    Daemon processes are usually started at system boot using init systems like systemd, init, or upstart.
  2. Stopping Daemons:
    Use commands like systemctl stop <daemon_name> or kill <PID> to terminate a daemon process gracefully.
  3. Monitoring Daemons:
    Tools like ps, top, or htop can be used to monitor the status and resource usage of daemon processes.
  4. Restarting Daemons:
    Restart a daemon process using systemctl restart <daemon_name> or similar commands if it becomes unresponsive.
  5. Log Management:
    Daemon processes typically write logs to files (e.g., /var/log/). Regular log monitoring helps detect issues or unusual behavior.
  6. Error Recovery:
    Configure watchdog systems to automatically restart daemons if they fail or crash unexpectedly.

Conclusion

Daemon processes are essential for managing background tasks and system services without user interaction. They are created by detaching a process from its terminal and parent, making them independent to run continuously in the background. By understanding their creation through steps like fork(), setsid(), and redirecting input/output, as well as through practical code examples, we can see how they ensure efficient system operations. From handling web servers to scheduling tasks, daemon processes play a critical role in maintaining smooth functionality and automation within an operating system.


Next Article

Similar Reads