Linux Starter Script: A simple log Check script

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.