In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to create a linux daemon". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to create a linux daemon.
In linux, a daemon, also known as a "wizard process", is a special process that runs in the background and is not controlled by any terminal, and is used to perform specific system tasks. The daemon is independent of the control terminal and periodically executes certain events.
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Definition of daemon
The daemon, also known as the Daemon process, is a special process that runs in the background and is not controlled by any terminal, and is used to perform specific system tasks. Many daemons start when the system boots and run until the system shuts 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. The daemon is a very useful process. Most of Linux's servers are implemented with daemons.
When the Linux system starts, it will start many system service processes. These system service processes have no control terminal and can not interact with users directly. Other processes are created when the user logs in or runs the program and terminates at the end of the run or when the user logs out, but the system service processes (daemons) are not affected by the user login and logout, and they are running all the time. This kind of process has a name called Daemon.
Let's use the ps axj command to view the processes on the system. Parameter a lists not only the processes of the current user, but also those of all other users; parameter x lists not only processes with control terminals, but also processes without control terminals; parameter j lists information related to job control.
(1) any TPGID column with-1 is a process without terminal control, that is, a daemon.
(2) in the COMMAND column, the kernel threads are indicated by the names enclosed in []. These threads are created in the kernel and have no user space code, so there is no program file name and command line. The name that begins with k is usually used to indicate Kernel.
(3) We are already familiar with the init process. Udevd is responsible for maintaining the device files in the / dev directory, acpid is responsible for power management, and syslogd is responsible for maintaining the log files under / var/log.
(4) as you can see, the daemon usually uses a name ending in d to denote Daemon.
Characteristics of daemons
(1) in Linux, the interface through which each system communicates with the user becomes a terminal, and every process running from this terminal is attached to this terminal, which is called the control terminal of these processes.
(2) when the control terminal is closed, the corresponding processes will shut down automatically. But the daemon can break through this restriction, it is separated from the terminal and runs in the background. (the purpose of leaving the terminal is to prevent the information of the process from being displayed in any terminal and the process will not be interrupted by the terminal information generated by any terminal.) it starts to run from the time it is executed and does not exit until the whole system shuts down (of course it can be thought of as killing the corresponding daemon)
(3) if you want a process not to be affected by users or interruptions or other changes, you must turn the process into a daemon.
Relationships between processes, process groups, sessions, and control terminals
Because the creation of daemons requires changing these environment parameters, it is important to understand the relationship between them:
Process group: it consists of one or more processes, and the process group number (GID) is the PID of the process leader in these processes.
Session: actually called session period (session), it includes all process groups during the period. Generally, a session period begins with the user login, and the login terminal is usually the shell terminal, so the shell terminal is the first process of this session period, and the session usually ends with logout. For a non-process team leader, it can call setsid () to create a new session.
Control terminal (tty): generally refers to the shell terminal, which may or may not be available during the session.
Create a daemon
The most critical step in creating a daemon is to call the setsid function to create a new Session Leader.
# includepid_t setid (void); / / the id of the newly created Session (that is, the id of the current process) is returned when the function is called successfully, and-1 is returned if an error occurs.
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, as long as you fork first and then call setsid. The child process created by fork and the parent process are in the same process group, and 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, and there will be no problem calling setsid in the child process.
The result of a successful call to this function is:
(1) create a new Session. The current process is Session Leader, and the id of the current process is the id of Session.
(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, it loses the terminal and becomes a process without a control terminal. The so-called loss of control terminal means that the original control terminal is still open and can still read and write, but it is only an ordinary open file, not a control terminal.
How to kill a daemon
1. First, ps axj | grep daemon name, find the corresponding daemon, and then use the kill-9 daemon name to kill
two。 Use the ps-ef command to find the corresponding daemon, and then kill it with the kill-9 command
3. Create shell scripts to automatically manage the startup, shutdown, and restart of the process.
Thank you for reading, the above is the content of "how to create a linux daemon". After the study of this article, I believe you have a deeper understanding of how to create a linux daemon, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.