Crontab for scheduled tasks in Linux

Source: Internet
Author: User

Crontab for scheduled tasks in Linux
What is CrontabCrontab? It is a scheduled task tool in Linux, which is equivalent to a task plan in WIN7, for details about how to enable system timing, refer to what CronCrontab can be used to add scheduled tasks in Linux or Unix systems, so that the system can regularly execute a command. For example, the database of the server needs to be restarted every morning, it is impossible to restart manually every morning. You can use Crontab to add a scheduled task for restarting the database at a.m.. The system automatically restarts the data at a.m. every day. Crontab can do a lot of things, such as sending mails at regular intervals, checking inventory at regular intervals, clearing logs at regular intervals, and backing up databases at regular intervals. In short, Crontab can be basically used to implement any task with periodic execution, of course, the premise is in the Linux system environment. Crontab simple use 1. common basic commands crontab-l list all scheduled tasks of the currently logged on user crontab-l-u XXX list all scheduled tasks of the XXX user, if no crontab for XXXcrontab-e is displayed, edit the current user's scheduled task crontab-r to delete the current user's scheduled task 2. scheduled task time setting * execution per minute */1 * execution every minute 0 5 * execution every five minutes 0-59/2 * execution every two minutes, execution is performed in an even number of minutes, such as 58/2, 61-*** every two minutes, and is performed in an odd number of minutes, such as, 70, 10 ** execute 0 0 1-5 on the 1st, 5th, and 10th of each month ** 3 on the 1st to 5th of each month. demo (1) run the command: crontab-e

(2) Enter ***** date>/tmp/date. log
(3) After saving, the system will prompt crontab: installing new crontab

(4) run the command: tail-f/tmp/date. log. The system prints the file date. log every minute.



Using Crontab + Shell to implement real-time monitoring of the system during the operation of the system website, we need to know whether the website or system is running normally for 24 hours. Generally, the normal operation of the system requires support from two things, one is a web server and the other is a database. Then we need to monitor whether the web server and database are running in real time. The following uses the apache server and mysql database as an example to analyze how to implement the requirements. 1. to monitor whether the mysql database is running normally, after logging on to a Linux server, we will execute pgrep Mysql to check whether the current mysql server is running, if a process ID is printed, mysql is running normally. Write the following SHELL script based on this principle:
#!/bin/bashcheck=`pgrep mysql`if [ -n "$check" ]; then    exitelse   date=$(date +"%Y-%m-%d %H:%M:%S")   /etc/init.d/mysqld restart   echo 'mysqld exception at' $date 'and restart 'fi

Determine whether the mysql process exists. If the process does not exist normally, mysql may be suspended. You can restart the process. Of course, you can directly send an email to the specified mailbox. 2. monitor whether apache runs normally. Based on mysql monitoring experience, we can write a SHELL script like this:
#!/bin/bashcheck=`pgrep httpd`if [ -n "$check" ]; then   exitelse   date=$(date +"%Y-%m-%d %H:%M:%S")  /etc/init.d/httpd restart   echo 'apache exception at' $date 'and restart 'fi



Another possibility is that apache may not be on the same server, which can be implemented using the following SHELL script:
if curl -m 10 -G localhost:81 > /dev/null 2>&1then      echo 'server is running'else      if ping -c 1 localhost > /dev/null 2>&1      then         echo 'server exception but ip ping success'      fifi

The principle is similar to directly accessing the URL to determine whether there is a response, and determining the apache port and the server IP address respectively. If the system access does not respond, the apache service may be suspended, and the server may be suspended. Therefore, PING the server to determine whether the server is normal. The last step is to add the preceding two scripts to the scheduled task for execution: crontab-e ***** sh/alidata/test/mysqlListener. sh>/var/log/mysqlL. log
* *** Sh/alidata/test/httpdListener. sh>/var/log/httpdL. log checks whether apache and mysql are running normally every minute, basically achieving real-time monitoring. If an email notification is added, as long as the system fails, the system will receive an email notification within one minute.

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.