Linux Daemon

Learn via video courses
Topics Covered

Overview

Have you ever wondered what those programs are running in the background of your Linux system, providing essential services like file sharing, web hosting, and email? These programs are called daemons.

What is a daemon in Linux? How can we create and manage daemon processes? What are some practical examples of using daemon commands? And finally, what are some of the daemon services that Linux offers?

By the end of this article, you will have answers to all of the above questions. Let's begin our expedition, shall we?

What is a Daemon Process in Linux?

Daemon in Linux is a background process designed to perform specific tasks or provide services without direct user intervention. Daemon

Exploring what is a daemon in Linux helps us grasp the concept of background services.

Here are some key characteristics of a daemon in Linux:

  • Daemons operate in the background, detached from any specific user session.
  • Daemons are responsible for providing essential services to the system or other processes. Examples include web servers (e.g., Apache), database servers (e.g., MySQL), network services (e.g., SSH), and logging services (e.g., rsyslogd).
  • Daemon processes are launched automatically by the system's initialization process or the init system in use (such as systemd, SysVinit, or Upstart).
  • During system startup, the init system reads its configuration files and starts the necessary daemons as specified.
  • Once a daemon process starts, it enters a loop where it waits for events or requests, processes them, and then continues waiting for events.
  • This event-driven design allows daemons to handle multiple events simultaneously and efficiently manage system resources.
  • Daemons may create child processes, fork new instances, or spawn worker threads to handle incoming requests or parallelize tasks effectively.
  • Don't get confused between programs, processes and services in Linux. Refer to the below table to understand the difference between them: Difference

Knowing what is daemon in Linux gives us insights into managing services efficiently.

Creating a Daemon Process on Linux

Creating a daemon process in Linux involves specific steps to ensure that the process runs independently in the background and provides the desired services.

Let's explore the process of creating a daemon in Linux with an example. In this example, the daemon will create a new file and write the current time to it every five seconds.

The code for the same is as follows:

Knowing what is daemon in Linux enables us to control and monitor vital system services. Now, follow the steps to create a daemon in Linux:

  1. Fork the Parent Proces:
    • The process begins with the parent process, and to create a daemon, a child process is forked from the parent. This is accomplished with the fork() system call.
  2. Exit on Fork Failure:
    • If the fork() system call fails, it returns a value of less than 0. If this happens, the program exits.
  3. Terminate the Parent Process:
    • If the fork() system call succeeds, the PID (Process ID) of the child process is returned in the parent, and 0 is returned in the child. If the process is the parent, then this code will exit.
  4. Create a New Session:
    • The setsid() system call is used to create a new session if the fork was successful. The child process becomes the leader of the new session and also becomes the process group leader of a new process group.
  5. Change Directory to Root:
    • Since a daemon process shouldn't use the file system, it should change its current working directory to root ("/"). This is accomplished using the chdir() system call.
  6. Close Standard File Descriptors:
    • A daemon process needs to close standard file descriptors to free them for reuse. This is achieved with the close() system call.
  7. Implement Daemon Functionality:
    • The infinite while loop is where the daemon process does its work. In this case, it writes the current time to a file every five seconds.

Let's shed light on what is a daemon in Linux by running the provided daemon process program written in C, you can follow these steps:

  1. Save the program code in a file, for example, time_daemon.c.
  2. Open a terminal and navigate to the directory where you saved the file.
  3. Compile the program using a C compiler. For example, if you have gcc installed, you can use the following command:
    Creating a Daemon Process on Linux GCC
  4. Start the daemon in Linux using the following command:
    Creating a Daemon Process on Linux Execute You can check that the daemon is running by executing the following command:
    Creating a Daemon Process on Linux PS You can also display the content of this file to see the daemon process in action. As you can see below, the daemon writes the current time to the file every five seconds. Creating a Daemon Process on Linux Dsiplay_Content
  5. You can stop the daemon process by using the following command:
    Creating a Daemon Process on Linux kill_all This command will send a signal to all processes named time_daemon. These processes will then terminate.

You may again execute the ps aux | grep time_daemon to check whether the daemon process is running or not.

Understanding what is a daemon in Linux and how to create it empowers us to harness the power of background processes.

Daemon Command Examples

Let's explore some practical examples of using the daemon command.

OptionDescriptionExample
daemon -- [command]Runs the command as a daemon.daemon -- sleep 60
daemon --name=[name] [command]starts a new process (a daemon) running the specified command in the background, and assigns the specified name to the daemon.daemon --name=sleepydaemon sleep 60
daemon --runningChecks if a daemon with the same name is already running.daemon --running --name=sleepydaemon
daemon --stop [--name=name]Stops a running daemon by name. (Assuming it was added in a newer version)daemon --stop --name=sleepydaemon
daemon --restart [--name=name]Restarts a running daemon by name. (Assuming it was added in a newer version)daemon --restart --name=sleepydaemon
daemon --respawn [command] Automatically restarts the command if it terminates.daemon --respawn sleep 60
daemon --chdir=[dir] [command]Changes the working directory of the daemon to the specified directory.daemon --chdir=/tmp sleep 60
daemon --user=[user] [command]Runs the command as a specified user.daemon --user=nobody sleep 60
daemon --pidfile=[file] [command]Writes the daemon’s pid to a specified file.daemon --pidfile=/var/run/sleepydaemon.pid sleep 60
daemon --inherit [command]Inherits the environment of the shell from which the daemon command was run.daemon --inherit --env=DISPLAY=:0.0 xclock
daemon --output=[file] [command]Redirects the command’s standard output and standard error to the specified file.daemon --output=/tmp/sleepydaemon.log sleep 60
daemon --unsafe [command]Runs the command before forking and releasing.daemon --unsafe sleep 60
daemon --limit=[resource=value] [command] Sets a resource limit for the daemon.daemon --limit=nofile=1024 sleep 60
daemon --delay=[sec] [command]Delays the start of the command by a given number of seconds.daemon --delay=10 sleep 60
daemon --env=[var=value] [command]Sets an environment variable for the daemon.daemon --env=PATH=/usr/local/bin sleep 60
daemon --errlog=[spec] [command]Logs error messages to a specified location.daemon --errlog=syslog sleep 60
daemon --dbglog=[spec] [command]Logs debug messages to a specified location.daemon --dbglog=syslog sleep 60
daemon --user=[user] [command]Runs the command as a specified user.daemon --user=nobody sleep 60
daemon --chroot=[dir] [command]Changes the root directory of the daemon to the specified directory.daemon --chroot=/tmp sleep 60
daemon --acceptable=[num] [command]Defines the number of respawns in a single run considered as acceptable before giving up.daemon --acceptable=5 --respawn sleep 60
daemon --attempts=[num] [command]Defines the number of attempts to respawn the command before giving up.daemon --attempts=5 --respawn sleep 60

Note: Most Linux distributions no longer recommend using the daemon command because of its legacy status. The systemctl command is now the preferred way to manage services. Nevertheless, you can still utilize the daemon command to initiate a process in the background.

Understanding what is daemon in Linux allows us to harness the power of continuous background processing.

List of Daemon Services for Unix and Linux Systems

Let's explore a curated list of commonly encountered daemons, each with its unique role and contribution:

  • cron:
    • The cron daemon in Linux is like your personal time manager. It schedules and executes tasks at specific times or intervals, allowing you to automate routine processes.
    • For example, you can set up cron jobs to perform regular backups or send email notifications.
  • sshd:
    • The sshd daemon in Linux enables secure SSH connections, allowing you to access your system remotely and transfer files securely.
  • httpd:
    • The powerful engine behind web servers, the httpd daemon delivers web pages to your browser.
    • It handles incoming web requests and serves requested content efficiently, like a skilled server catering to your online browsing needs.
  • mysqld:
    • Acting as the backbone of MySQL databases, the mysqld daemon in Linux manages and processes queries for reliable data storage and retrieval.
    • Think of it as a diligent librarian organizing and retrieving information from a vast collection of books.
  • named:
    • The named daemon in Linux, also known as BIND, plays a crucial role in domain name resolution.
    • It translates domain names into IP addresses and guides your browser to the correct web server.
  • rsyslogd:
    • Meet your system's dedicated logger. The rsyslogd daemon collects log messages generated by various processes and processes them.
    • It's like a diligent scribe, recording important events and providing insights for troubleshooting and system monitoring.
  • dbus-daemon:
    • Acting as a skilled mediator, the dbus-daemon in Linux enables the communication between different software components.
    • It facilitates seamless collaboration among applications, allowing them to coordinate actions and share information.

These daemons represent just a glimpse of the diverse services that power Unix and Linux systems. For this article, these are enough to learn what is a daemon in Linux.

Conclusion

In conclusion, we have explored daemon in Linux, its significance, and its functionality.

Let's summarize the key takeaways:

  • When it comes to Linux, knowing what is daemon becomes essential for background process management.
  • Daemons are essential background processes in Linux that operate autonomously, handling critical system functions without requiring direct user interaction.
  • The daemon command provides a convenient tool for managing and controlling daemon processes, allowing users to create, start, stop, and monitor them.
  • They differ from regular processes. Daemon in Linux handles critical system functions and often operates independently of user sessions.
  • Daemons can present debugging, management, and security challenges.

In conclusion, comprehending what is a daemon in Linux empowers users to efficiently manage background processes.