Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ crontab task scheduling Health Check     - Hadoop2.0 configuration yarn success (Server)

- Java reflection Introduction (Programming)

- RHEL5.8 physical opportunities to Read-only file system (Linux)

- Linux commands to access the cheat sheet (Linux)

- Ubuntu 14.04 Enable root and disable the guest (Linux)

- CentOS7 installation GAMIT and GMT (Linux)

- Can not remember how to solve the problem under Ubuntu brightness setting (Linux)

- Linux learning portal: CentOS 6.4 system kernel upgrade (Linux)

- Linux alpine use the command line to access Gmail (Linux)

- To configure and use multiple Tomcat under Linux environment (Server)

- Lazarus for Raspbian installation (Linux)

- Ubuntu Eclipse configuration Gtk + 2.0 libraries (Linux)

- Android webView URL redirects affect goBack () (Programming)

- Oracle: RETURNING clause (Database)

- Getting Started with Linux system to learn: how to install USB webcams come in raspberry (Linux)

- Detailed Linux platform chip programming process (Programming)

- How to achieve SSH without password login (Server)

- Linux LVM - File system extension (Linux)

- Linux Getting Started tutorial: XWindow what (Linux)

- PostgreSQL 9.3.2 Json type of use (Database)

 
         
  crontab task scheduling Health Check
     
  Add Date : 2017-01-08      
         
         
         
  System environment:

SUSE Linux Enterprise Server 10 SP1 (x86_64)

Background to the issue:

Because the system crontab content online environment more, during the daily crontab task scheduling, often abnormal hang up and affect the normal use of services, thus combining C and Shell to write a simple crontab health detection function.

Roadmap:
Modify the configuration parameters syslog, and the crontab schedule log separately extracted while adding a detectable label in the crontab entry, the periodic testing state flag by daemon process to determine whether the current crontab scheduling is normal, and in order to avoid the log file is too large to affect performance, log files regularly to make cutting and cleaning process.

# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

1, create the relevant directory

# Mkdir -p / data / logs / crontab

# Mkdir -p / data / scripts

# Mkdir -p / data / backup / crontab

 

# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

2, crontab health testing C code

# Cd / data / scripts

# Vim check_cron_process.h

#ifndef __CHECK_CRON_PROCESS_H__
#define __CHECK_CRON_PROCESS_H__
                                                                                 
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
                                                                                 
#define BUFFSIZE1 1024
#define BUFFSIZE2 32
                                                                                 
#define LOCKFILE "/var/run/check_cron_process.pid"
#define LOGFILE "/var/log/check_cron_process.log"
                                                                                 
#define LOCKMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
                                                                                 
#define SYSCMD1 "ps aux | grep -w cron | grep -v grep"
#define SYSCMD2 "ps aux | grep -w cron | grep -v grep | grep defunct"
#define SYSCMD3 "tail -6 /data/logs/crontab/cron.log | grep '(root) CMD (cd'"
#define SYSCMD4 "killall -9 cron> / dev / null 2> & 1"
#define SYSCMD5 "/ sbin / service cron start> / dev / null 2> & 1"
                                                                                 
void already_running (void);
void init_daemon (void);
int run_system_cmd (const char * syscmd);
                                                                                 
#endif
# Vim check_cron_process.c

#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};
                                                                             
/ * Get the current system time * /
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;
}
                                                                             
/ * Writes log file * /
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));
}
                                                                             
/ * * Only one copy of the run /
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 not 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 not lock% s:% s", LOCKFILE, strerror (errno));
        exit (1);
    }
                                                                                
    ftruncate (fd, 0);
    sprintf (buf, "% d", getpid ());
    write (fd, buf, strlen (buf));
    close (fd);
}
                                                                             
/ * Run as a daemon * /
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;
}
                                                                             
/ * Run the system command * /
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) {
        / * 1. Check the cron process is already running * /
        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;
        }
                                                                                    
        / * 2. If you have run, to see if there are zombie process * /
        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;
        }
                                                                                    
        / * 3. If the process is running correctly, check whether the normal task scheduling * /
        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;
        }
                                                                                    
        / * 4.crontab process operating normally * /
        writelog ( "! The cron process is ok \ n");
                                                                                    
CHECK_CRON:
        / * Sleep for 5 minutes, continue to detect * /
        sleep (300);
    }
                                                                                
    closelog ();
                                                                                
    return 0;
}
# Vim Makefile

CC = gcc
CXX = g ++
                                                                         
BINARY = check_cron_process
OBJS = check_cron_process.o
                                                                         
CFLAGS + = -I / usr / include -I / usr / local / include -Wall -Wno-unused-variable
LDFLAGS = -static -O2
                                                                         
all: $ (BINARY)
                                                                         
$ (BINARY): $ (OBJS)
    $ (CC) $ (LDFLAGS) -o $ (BINARY) $ (OBJS)
                                                                         
$ (OBJS):% o:.% C.
    $ (CC) $ (CFLAGS) -c $ ^ -o $ @
                                                                         
clean:
    rm -f $ (BINARY) $ (OBJS)
# Make

# / Data / scripts / check_cron_process

 

# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

3, crontab scheduling log configuration [New]

# Vim /etc/syslog.conf

## Check_crontab_start.tag.1
cron. * /data/logs/crontab/cron.log
## Check_crontab_end.tag.1
# Vim /etc/syslog-ng/syslog-ng.conf

## Check_crontab_start.tag.1
destination dst_cron {
    file ( "/ data / logs / crontab / cron.log");
};
                                                                   
log {
    source (src);
    filter (f_cron);
    destination (dst_cron);
};
## Check_crontab_end.tag.1
# / Sbin / service syslog restart


# ------------------------------------------------- -------------------------------------------------- -----------------------------------------

4, crontab task scheduling log processing

(1), the log cutting and cleanup

# Vim /data/scripts/cut_cron_log.sh

#! / Bin / bash
PATH = / sbin: / bin: / usr / sbin: / usr / bin: / usr / local / bin: / usr / local / sbin
                                                               
## MY CRON LOG PATH
LOGPATH = "/ data / logs / crontab"
                                                               
retval = `ps aux | grep sbin / cron | grep -v grep | wc -l`
if [$ {retval} -eq 0]; then
    echo "The cron process is not running ^ _ ^"
    exit 1
fi
                                                               
## Cut crontab's log
mv $ {LOGPATH} /cron.log $ {LOGPATH} / cron _ $ (date -d "yesterday" + "% Y-% m-% d"). log
/ Sbin / service syslog restart
## Clear 10 days ago's crontab logs
rm -f $ {LOGPATH} / cron _ $ (date -d "10 days ago" + "% Y-% m-% d"). log
(2), crontab backup information

# Vim /data/scripts/backup_crontab.sh

#! / Bin / bash
PATH = / sbin: / bin: / usr / sbin: / usr / bin: / usr / local / sbin: / usr / local / bin
                                                            
CRONTAB_BACKUP_DIR = "/ data / backup / crontab"
                                                            
# Crontab backup content
mkdir -p $ {CRONTAB_BACKUP_DIR}
crontab -uroot -l> $ {CRONTAB_BACKUP_DIR} / crontab_`date +% F`
                                                            
# Clean up before the backup 10
CRONBAK = crontab _ $ (date -d "10 days ago" + "% Y-% m-% d")
find $ {CRONTAB_BACKUP_DIR} -type f -name $ {CRONBAK} -exec rm -f {} \;

(3), crontab garbage clean-up header information

# Vim /data/scripts/clean_crontab_trash.sh

#! / Bin / bash
PATH = / sbin: / bin: / usr / sbin: / usr / bin: / usr / local / bin: / usr / local / sbin
                                                         
## The crontab's spool file
CRONFILE = "/ var / spool / cron / tabs / root"
sed -i '/ # DO NOT EDIT THIS FILE / d' $ {CRONFILE}
sed -i '/ # (\ / data \ /crontab.tmp/d' $ {CRONFILE}
sed -i '/ # (\ / tmp \ / crontab / d' $ {CRONFILE}
sed -i '/ # (Cron version / d' $ {CRONFILE}
sed -i '/ # (- installed on / d' $ {CRONFILE}
sed -i '/ # (\ / usr \ / local \ / agenttools / d' $ {CRONFILE}
sed -i '/ # (\ / tmp \ /cron.tmp/d' $ {CRONFILE}
sed -i '/ # (tmp2 installed / d' $ {CRONFILE}
sed -i '/ # (crontab.tmp / d' $ {CRONFILE}
sed -i '/ # (\ / data \ / crontab_ / d' $ {CRONFILE}
(4), crontab settings

# Crontab -e

## Crontab log cutting and cleanup

00 00 * * * /data/scripts/cut_cron_log.sh> / dev / null 2> & 1

## Health detectable label

* / 1 * * * * cd / usr / local; echo> / dev / null 2> & 1

## Crontab information backup

30 08 * * * /data/scripts/backup_crontab.sh> / dev / null 2> & 1

## Crontab spam headers cleanup

* / 30 * * * * /data/scripts/clean_crontab_trash.sh> / dev / null 2> & 1
     
         
         
         
  More:      
 
- How Glances monitoring system on Ubuntu (Linux)
- Examples 14 grep command (Linux)
- CentOS 7.0 running Docker kernel error solution (Server)
- Linux Samba server-side structures and the use of the client (Server)
- Specifies the open ports of the SUSE firewall settings (Linux)
- Win7 + Ubuntu Kylin + CentOS 6.5 installed three systems (Linux)
- Git Experience Sharing - Using a remote repository (Linux)
- RedHat Linux 6.5 Enterprise Edition installation Redis 3.0.3 (Database)
- BusyBox build root file system (Linux)
- The Linux-based security settings Ipchains Firewall (Linux)
- The principle Httpclient4.4 (execution request) (Programming)
- JEdit 5.2 Pro Edition installation on Ubuntu 14.04 (Linux)
- HBase vs Oracle (Database)
- Precautions against hackers Linux environment (Linux)
- Python kills corresponding process according to pid (Programming)
- Getting Started with Linux system to learn: how to install the Shrew Soft IPsec VPN on Linux (Linux)
- Android LayoutInflater source parsing (Programming)
- Vim Getting Started Tutorial (Linux)
- Linux print file and send mail (Linux)
- Redis is installed in Ubuntu 14.04 (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.