C++ 守护进程是';t与notify send交互
这是我的密码C++ 守护进程是';t与notify send交互,c++,c,linux,ipc,daemon,C++,C,Linux,Ipc,Daemon,这是我的密码 #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> #includ
#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>
#include <iostream>
int main(int argc, char *argv[]) {
pid_t pid, sid;
int sec = 10;
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0) {
std::cout << "Running with PID: " << pid << std::endl;
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0)
exit(EXIT_FAILURE);
if ((chdir("/")) < 0)
exit(EXIT_FAILURE); /* Log the failure */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
while (1) {
execl("/bin/notify-send", "notify-send", "-t", "3000", "Title", "body", NULL);
sleep(sec);
}
exit(EXIT_SUCCESS);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
pid_t pid,sid;
整数秒=10;
pid=fork();
if(pid<0){
佩罗尔(“福克”);
退出(退出失败);
}
如果(pid>0){
std::coutexecl
不会返回-它会用一个新的程序替换正在运行的程序。因此,在循环中使用sleep
运行它是没有意义的-它只会运行一次
我认为您应该使用系统
。它执行命令并返回
另一种方法是每次在循环中使用fork
,让子进程执行execl
,而父进程将继续循环。我认为在守护进程中运行它没有问题。系统受环境变量的影响,因此如果有人可以更改守护进程的环境,他可能会造成麻烦。但用户不应该这样做可以访问系统守护进程的环境。