这是我的守护进程:
#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$
我需要知道如何获取日志。