Linux守护程序不工作
Linux Daemon not working
我已经在c++中为linux创建了一个守护进程,但是,子进程似乎什么都没做。一旦到达if(pid>0)语句,一切似乎都停止了。Daemon.Start()的代码如下:
//Process ID and Session ID
pid_t pid,sid;
//Fork off the Parent Process
pid = fork();
if(pid < 0)
exit(EXIT_FAILURE);
//If PID is good, then exit the Parent Process
if(pid > 0)
exit(EXIT_SUCCESS);
//Change the file mode mask
umask(0);
//Create a new SID for the Child Process
sid = setsid();
if(sid < 0)
{
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);
//The main loop.
Globals::LogError("Service started.");
while(true)
{
//The Service task
Globals::LogError("Service working.");
if(!SystemConfiguration::IsFirstRun() && !SystemConfiguration::GetMediaUpdateReady())
{
SyncServer();
}
sleep(SystemConfiguration::GetServerConnectionFrequency()); //Wait 30 seconds
}
exit(EXIT_SUCCESS);
任何帮助都会很棒!:)
我很确定您的子进程在sid < 0
或chdir("/") < 0
if语句中死亡。在这些情况下,在退出前写入stderr以揭示问题所在:
//Create a new SID for the Child Process
sid = setsid();
if(sid < 0)
{
fprintf(stderr,"Failed to create SID: %sn",strerror(errno));
exit(EXIT_FAILURE);
}
//Change the current working directory
int chdir_rv = chdir("/");
if(chdir_rv < 0)
{
fprintf(stderr,"Failed to chdir: %sn",strerror(errno));
exit(EXIT_FAILURE);
}
您需要包括<errno.h>
和<string.h>
才能分别定义errno和strerror。
问候
相关文章:
- 如何使功能"run"工作。程序在调用运行时退出
- 任何人都有任何想法编写与C 旧手机中的消息作曲家以相同方式工作的程序
- 从守护程序启动的控制台应用捕获输出
- 使用 gdb 调试 linux 守护程序的初始启动
- 尝试将工作python程序转换为C++但不起作用
- 如何通过使用C,C 在Linux上指定其名称,以编程方式检查特定的守护程序
- 系统会冻结如果我使用运行的守护程序重新启动或关闭,该守护程序可以使用Fanotify控制对文件的访问
- 通过C 代码在Linux上启动/停止守护程序
- 当将应用程序作为启动的守护程序运行时,C++ 库中的信号 10 (SIGBUS)
- 程序的守护程序版本崩溃时的查找位置
- 在 Mac OSX 上打包 C++ 守护程序的推荐方法是什么?
- 将提升线程作为守护程序运行
- Windows 中守护程序的代码覆盖率
- C++:用于检测 USB 加密狗的守护程序
- C++守护程序静音模式
- 守护程序未与通知发送交互
- 仅在守护程序线程返回后删除它
- 开发 Mac OS 守护程序和 IPC
- Linux守护程序不工作
- 服务器守护程序的套接字停止工作