Lead to the task, to check daily scheduled tasks have no execution. Although simple but too cumbersome, wrote a script lazy. Learn several bash knowledge points by the way. Restudying it.
#检查前一天的crontab日志有没有执行JOB1和JOB2两个定时任务 # Check that the data in the database is cleaned up in time #!/bin/bashdeclare -i db_status=0# because the log date is only valid for month and day , time is not judged, so the time output is formatted using the date -d parameter. #由于date命令用于grep, with a space in the middle, cannot be directly spliced, you must first assign a variable, and then use double quotation marks to label the variable as the grep parameter declare yesterday= ' Date -d last-day + "%b %d" ' declare perhour_job= "JOB1" declare perday_job= "JOB2" Cat /var/log/cron|grep "$YESTERDAY" |grep "$PERHOUR _job" >/dev/null 2>&1if [ $? -eq 1 ];then echo "per hour job failed." else echo "Per hour job success." ficat /var/log/cron|grep "$YESTERDAY" |grep "$PERDAY _job" >/dev/null 2>&1if [ $? -eq 1 ];then echo "per day job failed." else echo "Per day job success." fi# switches the user to execute the script, using the Su - username -c parameter, directly with the entire command line. You need to enclose the entire command in double quotation marks # Check the database for temporary tables there is no content, no word is normal. Some words spool the content to CHK_PERDAY.LOGSU&NBSP;-&NBSP;ORACLE&NBsp;-c "Sqlplus username/userpassword @/tmp/chk_perday.sql >/dev/null 2>&1" # Note: When you need to assign a value to an external variable within a loop, you cannot use the Command|while read line format # You must first save the output to a temporary file and then the last done < file name. #这是因为前者while调用子进程, the assignment within the child process is not brought into the parent variable, the latter being in the unified process, the assignment is normal. while read xmdo if [ xm != null ];then db_status=1 echo "$XM transfer failed" fidone < /tmp/chk_perday.logif [ $ db_status = 0 ];then echo "Data transfer success." else echo "data transfer failed." Fiexit
This article is from "Balsam Pear" blog, please make sure to keep this source http://golehuang.blog.51cto.com/7499/1839611
Linux Starter Script: A simple log Check script