📄 cron.c
字号:
(void) snprintf(n, MAX_FNAME, CRON_TAB(User)); if (!(crontab = fopen(n, "r"))) { /*no crontab before*/ /*no such file or dir*/ if (errno != ENOENT) { perror(n); snprintf(error,MAX_LINE,"%s: can not open file %s.",ProgramName,n); log_err(error); return(0); } fprintf(stderr, "no crontab for %s - using an empty one\n",User); if (!(crontab = fopen("/dev/null", "r"))) { snprintf(error,MAX_LINE,"%s: can not open /dev/null.",ProgramName); log_err(error); return(0); } } del_rec(); /*----------------------------------------------*/ /*************** load the date ***************/ while(fgets(line,MAX_LINE,crontab) != NULL) { if(!add_rec()) return 0; current_date = head; can_gets = 1; pt = 0; /****** to see if the line is an empty line or a comment line **/ while(line[pt] == ' ' || line[pt] == '\t') pt++; if(line[pt] == '#' || line[pt] == '\n') continue; /*************** divide the date in to five part *****************/ for(; i < 5; i++) { finish = 0; while(1) { flag = get_next_token(buffer,line,&pt); switch(flag) { case 1: if((num = check_number(buffer,i)) < 0) { return 0; } record_it(current_date,i,num); finish = 1; break; case 2: record_it(current_date,i,'*'); finish = 1; break; case 3: if((num = check_number(buffer,i)) < 0) { return 0; } record_it(current_date,i,num); finish = 0; break; case 4: case 5: default: return 0; break; } if(finish) break; } } strncpy(current_date->command,&line[pt],MAX_LINE); i = 0; } fclose(crontab); return(1);}void cron_sleep(int target){ int seconds_to_wait; seconds_to_wait = (int)(target - time((time_t*)0)) + 1; if (seconds_to_wait > 0 && seconds_to_wait< 65) sleep((unsigned int) seconds_to_wait);}static int do_job(time_t current_time){ struct tm *tm = localtime(¤t_time); char error[MAX_LINE]; date_rec * pt = head; while(pt) { if(/*pt->minute_rec[tm->tm_min] && pt->hour_rec[tm->tm_hour] && pt->day_of_mon_rec[tm->tm_mday] && pt->mon_of_year_rec[tm->tm_mon] && pt->day_of_week_rec[tm->tm_wday]*/ CHECK(pt->minute_rec,tm->tm_min)&&CHECK(pt->hour_rec,tm->tm_hour)&& CHECK(pt->day_of_mon_rec,tm->tm_mday)&&CHECK(pt->mon_of_year_rec,tm->tm_mon) &&CHECK(pt->day_of_week_rec,tm->tm_wday)) { switch(fork()) { case -1: snprintf(error,MAX_LINE,"%s: fork error.",ProgramName); log_err(error); return 0; case 0: /* child */ execlp("bash","bash","-c",pt->command,(char *)0); snprintf(error,MAX_LINE,"%s: execlp %s.",ProgramName,pt->command); log_err(error); break; default: /* parent */ break; } } pt = pt->next; } return 1;}static void sigchld_handler(int x) { int save_errno = errno; int waiter; pid_t pid; for (;;) { pid = waitpid(-1, &waiter, WNOHANG); switch (pid) { case -1: errno = save_errno; return; case 0: errno = save_errno; return; default: break; } } errno = save_errno;}int main(){ /*---------------------------------------------------------------------------*/ int ok = 1; int running_time; int modify_time; int check_time; int markfd; int check; char error[MAX_LINE]; char n[MAX_FNAME]; char * User; struct stat buf; struct passwd *pw; /*************** handle signal here ***************/ //(void) signal(SIGCLD, SIG_IGN); (void) signal(SIGTERM, SIG_IGN); //(void) signal(SIGCHLD, sigchld_handler); //if(daemon_init() < 0) // log_err("Daemon fork fail"); /*************** the body ***************/ /* open the crontab */ if (!(pw = getpwuid(getuid()))) { snprintf(error,MAX_LINE,"%s: your UID isn't in the passwd file.\n", ProgramName); log_err(error); return(0); } /* dup the user id*/ if (((User=strdup(pw->pw_name)) == NULL)) { snprintf(error,MAX_LINE, "%s: Memory allocation error\n",ProgramName); log_err(error); return(0); } (void) snprintf(n, MAX_FNAME, CRON_TAB(User)); if((markfd = open(n,O_RDONLY, 0400)) == -1) { modify_time = time(0); check_time = modify_time; } else { /*************** get the last modify time the mark file ***************/ if((check = fstat(markfd,&buf)) < 0) { snprintf(error,MAX_LINE,"%s: stat fail!",ProgramName); log_err(error); exit(1); } close(markfd); modify_time = buf.st_mtime; check_time = modify_time; } /*************** load the command ***************/ if(!load_rec()) { ok = 0; snprintf(error,MAX_LINE,"%s: load record from crontab fail!",ProgramName); log_err(error); } running_time = time(0); while(1) { if((markfd = open(n,O_RDONLY, 0400)) > 0) { /******** check if there is any change of crontab **********/ if((check = fstat(markfd,&buf)) < 0) { snprintf(error,MAX_LINE,"%s: stat fail!",ProgramName); log_err(error); exit(1); } check_time = buf.st_mtime; } /*************** if so load the command again ***************/ if(modify_time != check_time) { snprintf(error,MAX_LINE,"%s: load record from crontab again!",ProgramName); log_err(error); if(!load_rec()) { ok = 0; snprintf(error,MAX_LINE,"%s: load record from crontab fail!",ProgramName); log_err(error); } modify_time = check_time; } if(ok) { /* do the job */ do_job(running_time); } else exit(1); /* sleep */ running_time += SECONDS_PER_MINUTE; cron_sleep(running_time); close(markfd); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -