Home >Operation and Maintenance >Linux Operation and Maintenance >what is linux daemon
In Linux, the daemon process is also called the "elf process". It is a special process that runs in the background and is not controlled by any terminal. It is used to perform specific system tasks. The daemon process is independent of the controlling terminal and periodically executes certain events as they occur.
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Definition of daemon process
The daemon process is also called a daemon process (Daemon). It is a process that runs in the background and is not controlled by any terminal. A special process used to perform specific system tasks. Many daemons are started when the system boots and run until the system is shut down. Others only start when needed and end automatically when the task is completed.
It is independent of the control terminal and periodically executes certain events that occur. Daemon is a very useful process. Most Linux servers are implemented using daemon processes.
When the Linux system starts, many system service processes will be started. These system service processes do not have control terminals and cannot directly interact with users. Other processes are created when the user logs in or runs the program, and terminates when the operation ends or the user logs out, but the system service process (daemon process) is not affected by the user logging in and out, and they are always running. This kind of process has a name called daemon.
Let’s use the ps axj command to view the processes in the system. The parameter a indicates not only the processes of the current user, but also the processes of all other users; the parameter x indicates not only the processes with a controlling terminal, but also all processes without a controlling terminal; the parameter j indicates the listing and job control Related information.
(1) Anything with -1 in the TPGID column is a process that does not control the terminal, that is, a daemon process;
(2) In The names enclosed in [ ] in the COMMAND column represent kernel threads. These threads are created in the kernel and have no user space code, so there are no program file names and command lines. Names starting with k are usually used to represent Kernel;
(3) We are already familiar with the init process. udevd is responsible for maintaining device files in the /dev directory, acpid is responsible for power management, and syslogd is responsible for maintaining log files under /var/log;
(4) You can see Note that daemon processes usually have names ending in d, which means Daemon.
Characteristics of daemon process
(1) In Linux, the interface for each system to communicate with users becomes a terminal, and every process running from this terminal will Attached to this terminal, this terminal is called the control terminal of these processes;
(2) When the control terminal is closed, the corresponding processes will automatically close. But the daemon process can break through this limitation. It is separated from the terminal and runs in the background. (The purpose of being separated from the terminal is to prevent the information during the running process from being displayed in any terminal and the process will not be spawned by any terminal. Interrupted by terminal information), it starts running when it is executed and does not exit until the entire system is shut down (of course it can be considered as killing the corresponding daemon process);
(3) If you want a certain If the process is not affected by users or interruptions or other changes, then the process must be turned into a daemon process.
The relationship between processes, process groups, sessions, and control terminals
Because the creation of a daemon process requires changing these environmental parameters, it is very important to understand the relationship between them. Important:
Process group: It is composed of one or more processes, and the process group number (GID) is the process group leader of these processes PID.
Session: Actually called session, it includes all process groups during the period. Generally, a session starts when the user logs in. Generally, the login is a shell terminal, so the shell terminal It is also the first process of this session, and the session usually ends with logout. For a non-process leader, it can call setsid() to create a new session.
Control terminal (tty): generally refers to the shell terminal, which may or may not exist during the session.
Create a daemon process
The most critical step in creating a daemon process is to call the setsid function to create a new Session Leader .
#includepid_t setid(void);//该函数调用成功时返回新创建的Session的id(其实也就是当前进程的id),出错返回-1。
Note that before calling this function, the current process is not allowed to be the leader of the process group, otherwise the function returns -1. It is also easy to ensure that the current process is not the leader of the process group. Just fork first and then call setsid. The child process created by fork is in the same process group as the parent process. The Leader of the process group must be the first process of the group, so the child process cannot be the first process of the group. Just call setsid in the child process. There will be no problem.
The result of successfully calling this function is:
(1) Create a new Session, the current process is the Session Leader, and the id of the current process is the Session id;
(2) Create a new process group, the current process is the Leader of the process group, and the ID of the current process is the ID of the process group;
(3) If the current process originally has a control terminal, then It loses this terminal and becomes a process without a controlling terminal. (The so-called lost control terminal means that the original control terminal is still open and can still be read and written, but it is just an ordinary open file, not a control terminal).
How to kill the daemon process
1. Firstps axj | grep daemon name
to find the corresponding daemon process, and then use kill -9 daemon process name
to kill;
2. Use the ps -ef
command to find the corresponding daemon process, and then use kill -9
command to kill it;
3. Create a shell script to automatically manage the startup, shutdown, and restart of the process.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of what is linux daemon. For more information, please follow other related articles on the PHP Chinese website!