1. 为了便于进行调试,打开syslog。
2. 打开syslog后,可以在/var/log/message中看到保存的log信息。
3. 找到程序的pid后,可以用过kill pid方式杀死daemon,同样可以在log信息中有所显示。
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include <stdio.h> #include <syslog.h> #include <signal.h>
int daemon_init (void) { pid_t pid; if ((pid = fork ()) < 0) return (-1); else if (pid != 0) exit (0); /* parent exit */ /* child continues */ setsid (); /* become session leader */ chdir ("/"); /* change working directory */ umask (0); /* clear file mode creation mask */ close (0); /* close stdin */ close (1); /* close stdout */ close (2); /* close stderr */ return (0); }
void sig_term (int signo) { if (signo == SIGTERM) /* catched signal sent by kill(1) command */ { syslog (LOG_INFO, "program terminated."); closelog (); exit (0); } }
int main (void) { if (daemon_init () == -1) { printf ("can't fork self\n"); exit (0); } openlog ("daemontest", LOG_PID, LOG_USER); syslog (LOG_INFO, "program started."); signal (SIGTERM, sig_term); /* arrange to catch the signal */ while (1) { sleep (1); /* put your main program here */ } return (0); }
|
阅读(1464) | 评论(0) | 转发(0) |