crontab任务调度健康检测_Linux教程_Linux公社

Linux系统门户网站

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/param.h>#include <sys/stat.h>#include <syslog.h>#include <fcntl.h>#include <errno.h>#include <time.h>#include "check_cron_process.h"static char buffer[BUFFSIZE1] = {0};static char datetime[BUFFSIZE2] = {0};int get_curr_date(char *strtime, unsigned int ustrlen){    struct tm *pt = NULL;    time_t timer;    if (!strtime) {        return -1;    }    time(&timer);    strtime[0] = '\0';    pt = localtime(&timer);    if (!pt) {        return -1;    }    memset(strtime, 0, ustrlen);    sprintf(strtime, "%04d-%02d-%02d-%02d:%02d:%02d",        pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday, pt->tm_hour, pt->tm_min, pt->tm_sec);    return 0;}int writelog(const char *pLoginfo){    FILE *fp = NULL;    unsigned int ustrlen = 0;    if (pLoginfo == NULL) {        return -1;    }    ustrlen = strlen(pLoginfo);    if (ustrlen > 256) {        return -1;    }    if ((fp = fopen(LOGFILE, "a+")) == NULL) {        return -1;    }    memset(datetime, 0, BUFFSIZE2);    get_curr_date(datetime, BUFFSIZE2);    fprintf(fp, "%s   %s", datetime, pLoginfo);    fclose(fp);    return 0;}int LockFile(int fd){    struct flock fl;    fl.l_type = F_WRLCK;    fl.l_start = 0;    fl.l_whence = SEEK_SET;    fl.l_len = 0;    return (fcntl(fd, F_SETLK, &fl));}void already_running(void){    int fd = -1;    char buf[16] = {0};    fd = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);    if (fd < 0) {        syslog(LOG_ERR, "can't open %s: %s", LOCKFILE, strerror(errno));        exit(1);    }    if (LockFile(fd) < 0) {        if (errno == EACCES || errno == EAGAIN) {            close(fd);            exit(1);        }        syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno));        exit(1);    }    ftruncate(fd, 0);    sprintf(buf, "%d", getpid());    write(fd, buf, strlen(buf));    close(fd);}void init_daemon(void){    int pid = -1;    if ((pid = fork())) {        exit(0);    } else if (pid < 0) {        exit(1);    }    setsid();    if ((pid = fork())) {        exit(0);    } else if (pid < 0) {        exit(1);    }    chdir("/tmp");    umask(0);    return;}int run_system_cmd(const char *syscmd){    FILE *fp = NULL;    if (syscmd == NULL) {        return -1;    }    memset(buffer, 0, BUFFSIZE1);    snprintf(buffer, BUFFSIZE1, syscmd);    fp = popen(buffer, "r");    if (!fp) {        return 0;    }    memset(buffer, 0, BUFFSIZE1);    if (!fgets(buffer, BUFFSIZE1, fp)) {        pclose(fp);        return 0;    }    if (!strncasecmp(buffer, "", BUFFSIZE1)) {        pclose(fp);        return 0;    }    pclose(fp);    return 1;}int main(int argc, char *argv[]){    int ret = 0;    init_daemon();    already_running();    openlog(NULL, LOG_CONS | LOG_PID, LOG_LOCAL1);    while(1) {        ret = run_system_cmd(SYSCMD1);        if (!ret) {            writelog("The cron process is not running, now start it! \n");            sleep(1);            system(SYSCMD5);            goto CHECK_CRON;        }        ret = run_system_cmd(SYSCMD2);        if (ret) {            writelog("The cron process is defunct, now restart it! \n");            sleep(1);            system(SYSCMD4);            sleep(1);            system(SYSCMD5);            goto CHECK_CRON;        }        ret = run_system_cmd(SYSCMD3);        if (!ret) {            writelog("The cron work is down, now restart it! \n");            sleep(1);            system(SYSCMD4);            sleep(1);            system(SYSCMD5);            goto CHECK_CRON;        }        writelog("The cron process is ok! \n");CHECK_CRON:        sleep(300);    }    closelog();    return 0;}
本文由 黑白世界4648 第一时间收藏到GET,原文来自 → www.linuxidc.com

「GetParty」

关注微信号,推送好文章

微信中长按图片即可关注

更多精选文章

评论
微博一键登入