可安装的 Linux 守护进程,如何在 C++ 中创建它们?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5729230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installable Linux daemons, how to create them in C++?
提问by charfeddine.ahmed
Installable Linux daemons, how to create them in C++ ?
可安装的 Linux 守护进程,如何在 C++ 中创建它们?
I have a server applicationin C++ which I want it to behave the same way as a Windows Service. Ie it should be started whenever the system is started independently of whether there is a user logged in or not. In Windows there are a lot of C++ classes able to facilitate communication with the Service manager and handle the start/stop/pause commands. What about Linux ? Also how can I easily deploy my application ? I envy the way how some applications get installed using the apt-get system command, is that easy to do with a custom application, say that I provide the binaries in one machine then automatically fetch them from the target Linux one ... ?
我有一个 C++ 服务器应用程序,我希望它的行为方式与 Windows 服务相同。即它应该在系统启动时启动,而不管是否有用户登录。在 Windows 中,有很多 C++ 类能够促进与服务管理器的通信并处理启动/停止/暂停命令。Linux 呢?另外,如何轻松部署我的应用程序?我羡慕使用 apt-get system 命令安装某些应用程序的方式,使用自定义应用程序是否容易做到,比如说我在一台机器上提供二进制文件,然后自动从目标 Linux 中获取它们......?
Thank you in advance for your help.
预先感谢您的帮助。
采纳答案by charfeddine.ahmed
Ok, the first thing, you need to know that writing services in Windows and Linux is very different. First of all, in Linux, "services" are not called "services", they're called "daemons". Knowing that, you can use google to find this extremely useful document.
好的,首先,你需要知道在 Windows 和 Linux 中编写服务是非常不同的。首先,在 Linux 中,“服务”不称为“服务”,而是称为“守护进程”。知道了这一点,您可以使用 google找到这个非常有用的文档。
As for starting/stopping/restarting, there is no universal premade solution here. In most cases, daemons create *.pid files in /var/run; those files contain their process identifiers "PIDs". Then a simple bash script is written that controls the daemon execution by reading the pid from the appropriate file and sending a kill signal to it.
至于启动/停止/重启,这里没有通用的预制解决方案。在大多数情况下,守护进程在 /var/run 中创建 *.pid 文件;这些文件包含它们的进程标识符“PID”。然后编写一个简单的 bash 脚本,通过从适当的文件读取 pid 并向其发送终止信号来控制守护进程的执行。
For example, suppose your daemon name is foo
. Then it will create the file /var/run/foo.pid and write its PID into it, using ASCII characters and appending a newline at the end. Your control script name would be fooctl
, it should support the following commands: start, stop and restart. That is, when you run fooctl start
, the script should first check if the corresponding pid file exists, if not, then do whatever is necessary to start the daemon; when you run fooctl stop
, it should read the pid from /var/run/foo.pid and kill the process with that ID. In case of fooctl restart
, your script will need to first stop the daemon, then start it again.
例如,假设您的守护程序名称是foo
. 然后它将创建文件 /var/run/foo.pid 并将其 PID 写入其中,使用 ASCII 字符并在末尾附加换行符。您的控制脚本名称为fooctl
,它应支持以下命令:启动、停止和重新启动。也就是说,当你运行时fooctl start
,脚本首先要检查对应的pid文件是否存在,如果不存在,则做任何必要的事情来启动守护进程;当您运行时fooctl stop
,它应该从 /var/run/foo.pid 读取 pid 并使用该 ID 终止进程。在 的情况下fooctl restart
,您的脚本需要先停止守护进程,然后再次启动它。
That all being said, it is just an agreement about how daemons should work. This is how it's usually done. But those rules are not enforced in any way. You are free to invent and use your own techniques for creating and controlling daemons.
话虽如此,这只是关于守护进程应该如何工作的协议。这是通常的做法。但这些规则并没有以任何方式强制执行。您可以自由地发明和使用自己的技术来创建和控制守护进程。
As for the second part of your question (about apt-get), this is called package management. It has nothing to do with daemons, but since you asked: to do it with your custom application, you would need to publish it in the main repository, which might be impossible for a number of reasons; or you can create your own repo. Also you can assemble a *.deb package for your application and it will be just as easy to install. Search the web for more info on how to build packages for custom Linux applications.
至于您问题的第二部分(关于 apt-get),这称为包管理。它与守护进程无关,但由于您要求:要使用自定义应用程序来执行此操作,您需要将其发布到主存储库中,这可能由于多种原因而无法实现;或者您可以创建自己的回购。你也可以为你的应用程序组装一个 *.deb 包,它也很容易安装。在网络上搜索有关如何为自定义 Linux 应用程序构建包的更多信息。
回答by charfeddine.ahmed
In linux, daemon is essentially interfaceless program. It has no special differences in code. Making a program to autostart as a daemon is, unfortunately, done in different ways on different distros. For Ubuntu, read about runlevels and upstart/plymouth system. You have to build a package file that does the setup for you, not the program.
在 linux 中,daemon 本质上是无接口程序。它在代码上没有特别的区别。不幸的是,将程序作为守护程序自动启动在不同的发行版上以不同的方式完成。对于 Ubuntu,请阅读有关运行级别和 upstart/plymouth 系统的信息。您必须构建一个包文件来为您进行设置,而不是程序。
回答by AProgrammer
Deamons are process like others. Excepted that they have no parent (or their parent is process 0), no group id and no session id. Richard Stevens (whose Advanced Programming in the Unix Environmentis still recommended when you are doing system programming under Unix) gives this function as a way to set up things:
守护进程和其他进程一样。除非他们没有父级(或者他们的父级是进程 0),没有组 ID 和会话 ID。Richard Stevens(当你在 Unix 下进行系统编程时,仍然推荐他在 Unix 环境中进行高级编程)给出了这个函数作为一种设置方式:
if ( (pid = fork()) < 0) {
return -1;
else if (pid == 0) {
exit(0);
setsid();
chdir("/");
umask(0);
but it still doesn't handle some issues (closing of inherited file handles for instance, the subject is handled somewhere else in the book).
但它仍然没有处理一些问题(例如,关闭继承的文件句柄,该主题在书中的其他地方处理)。
Then there is the problem of controlling the deamon. Some conventions are common (but not universal):
然后是控制守护进程的问题。一些约定是常见的(但不通用):
- writing its pid in a file under /var/run/
- handling SIGHUP as a way to reread its configuration file
- using the syslog system to log messages
Linux distributions have also common ways to control the deamons. Those are usually handled via scripts installed with the deamon, which translate the distribution conventions to what is precisely used by the deamon.
Linux 发行版也有控制守护进程的常用方法。这些通常通过随守护程序安装的脚本处理,这些脚本将分发约定转换为守护程序准确使用的内容。
回答by Elshan
Try with this link.It's contain A-Z daemon writing.
尝试使用此链接。它包含 AZ 守护程序编写。
First off, you'll need the following packages installed on your Linux machine to develop daemons, specifically:
GCC 3.2.2or higher and Linux Development headers and libraries
首先,您需要在 Linux 机器上安装以下软件包来开发守护进程,特别是:
GCC 3.2.2或更高版本以及Linux 开发头文件和库
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
int main(void) {
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
/* The Big Loop */
while (1) {
/* Do some task here ... */
sleep(30); /* wait 30 seconds */
}
exit(EXIT_SUCCESS);
}