我的誓言就是:
#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$
我需要知道如何获得这一记录。