下面是我的守护进程:
#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>
using namespace std;
#define DAEMON_NAME "vdaemon"
void process(){
syslog (LOG_NOTICE, "Writing to my Syslog");
}
int main(int argc, char *argv[]) {
//Set our Logging Mask and open the Log
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
syslog(LOG_INFO, "Entering Daemon");
pid_t pid, sid;
//Fork the Parent Process
pid = fork();
if (pid < 0) { exit(EXIT_FAILURE); }
//We got a good pid, Close the Parent Process
if (pid > 0) { exit(EXIT_SUCCESS); }
//Change File Mask
umask(0);
//Create a new Signature Id for our child
sid = setsid();
if (sid < 0) { exit(EXIT_FAILURE); }
//Change Directory
//If we cant find the directory we exit with failure.
if ((chdir("/")) < 0) { exit(EXIT_FAILURE); }
//Close Standard File Descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
//----------------
//Main Process
//----------------
while(true){
process(); //Run our Process
sleep(2); //Sleep for 2 seconds
}
//Close the log
closelog ();
}我把它编译成,
gcc vdaemon.cpp -o vdaemon并运行它
./vdaemon 然后当我得到
shishir@dewsworld:~/daemon$ ps -A | grep --color='auto' "vdaemon"
5060 ? 00:00:00 vdaemon然后我就得不到日志
shishir@dewsworld:~/daemon$ dmesg | grep --color='auto' "Writing to my Syslog"
shishir@dewsworld:~/daemon$ 我需要知道怎么弄到日志。
发布于 2012-05-11 15:49:57
建议您查看/var/log/messages区域,了解syslog守护进程将写入的内容。如果您仍然无法在那里找到消息,那么您应该查看syslog守护进程的配置。它可以丢弃LOG_NOTICE消息或LOG_USER消息。您可以在系统级别配置syslog守护程序报告的内容。
https://stackoverflow.com/questions/10545301
复制相似问题