用 C 创建一个具有停止、启动功能的守护进程
如何在该守护进程代码中添加守护进程停止、启动和报告功能?
#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);
}
How to add daemon stop, start and report function to this daemon code?
#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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/var/run/mydaemonname.pid
,以便以后方便查找pid。停止/启动存在一些复杂性,但如果我正确理解了这个问题,这应该会让您走上正确的轨道。
编辑:按照 Dummy00001 在下面的评论中的建议添加了 pid 文件。
/var/run/mydaemonname.pid
so that you can easily look up the pid later.There are some complications around stop/start, but if I'm understanding the question correctly, this should get you on the right track.
Edit: Added pid file as suggested by Dummy00001 in a comment below.
首先,您可能不必自己做那么多的分叉和整理工作: http://linux .die.net/man/3/daemon
接下来,请记住,您的守护进程与世界的接口可能是通过某种 shell 脚本来实现的,您也可以在 /etc/init.d 或任何其他发行版定义的位置编写该脚本地方。
因此,对于上述答案,您的 shell 脚本会将这些信号发送到进程的 pid。但可能有更好的方法。像上面这样的信号是一个单向过程,您的控制脚本必须跳过容易出现竞争条件且脆弱的循环,以确认守护进程是否成功停止或重新启动。我会在 /etc/init.d 中查找优先级和示例。
First, you probably don't have to do so much forking and house keeping yourself: http://linux.die.net/man/3/daemon
Next, remember that your daemon's interface to the world is probably through some sort of shell script that you also write located in /etc/init.d or whatever other distro-defined place.
So for the above answer, your shell script would send those signals to the pid of the process. There probably is a better way though. Signaling like above is a one way process, your controlling script has to jump through race-condition-prone and fragile hoops in order to confirm if the daemon successfully stopped or restarted. I would look for precedence and examples in /etc/init.d.