使用tail -f /var/log/messages 查看执行情况
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <syslog.h>
- #define INTERVAL 3600*24
- void sigalrm_fn(int sig)
- {
- static int count;
- char *name="XinYiWuXian";
- open(name,LOG_CONS|LOG_PID,LOG_USER);
- syslog(LOG_INFO,"%d,log info...",++count);
- closelog();
- alarm(INTERVAL);
- return;
- }
- int
- daemon(int nochdir,int noclose)
- {
- pid_t pid;
- pid=fork();
- if(pid<0)
- {
- perror("fork");
- return -1;
- }
- if(pid!=0)
- {
- exit(0);
- }
- pid=setsid();
- if(pid<-1)
- {
- perror("setsid");
- return -1;
- }
- if(!nochdir)
- chdir("/");
- if(!noclose)
- {
- int fd;
- fd=open("/dev/null",O_RDWR,0);
- if(fd!=-1)
- {
- dup2(fd,STDIN_FILENO);
- dup2(fd,STDOUT_FILENO);
- dup2(fd,STDERR_FILENO);
- if(fd>2)
- close(fd);
- }
- }
- umask(0027);
- return 0;
- }
- int
- main(void)
- {
- daemon(0,0);
- signal(SIGALRM,sigalrm_fn);
- alarm(5);
- while(1)
- {
- pause();
- }
- return 0;
- }