|
What is the Crontab
Crontab is a regular task tool under Linux system, the equivalent of WIN7 system mission plan, allowing the system time to make a detailed Cron
What can be done Crontab
Crontab can be used to add a Linux or Unix system scheduled tasks, so that the system timing to execute a command, such as the database server needs to restart the next day in the morning, every morning it is impossible to manually restart. You can use Crontab Timing task add 5:00 to restart the database, the system every day to 5:00 will automatically restart the data.
Crontab still many things to do, such as sending e-mail regularly, regular inspection and inventory, clean up the log regularly, regular backup of the database, in short, as long as it has the basic task performed periodically Crontab can be used to achieve, of course, provided in the Linux system environment.
Crontab simple to use
1. Common basic commands
crontab -l lists all the scheduled tasks currently logged in user
crontab -l -u XXX XXX user lists all the scheduled tasks, if there is no will prompt no crontab for XXX
crontab -e to edit the user's current scheduled tasks
crontab -r Remove the current user's regular tasks
2. The timing of the task set time
* * * * * Per minute
* / 1 * * * * per minute
05 * * * at five o'clock every execution
0-59 / 2 * * * * every two minutes to perform, and are even a few minutes to perform, such as 2,4,6
1-58 / 2 * * * * every two minutes to perform, and the odd few minutes to perform, such as 3,5,7
00 No. 1, 5, 10 * * per month, on the 5th, 10th execution
001-5 * * 1-5 month implementation of Hao
3.Demo
(1) execute the command: crontab -e
(2) Input * * * * * date >> /tmp/date.log
(3) After saving, the system will prompt crontab: installing new crontab
(4) Run: tail -f /tmp/date.log system will be able to see every minute of time to print out this document date.log
Crontab + Shell with real-time monitoring system
In the operation process of the system site, we need to know whether the site or system 24 hours in normal operation, the normal operation of the system generally requires the support of two things, one is the web server, a database.
Then we need real-time monitoring web server and database is running. Let's analyze how needs to apache and mysql database server as an example.
1. Monitoring mysql database running normally
Normally after we log on a Linux server, you want to view the current Mysql server is running, we'll execute pgrep mysql view, if you have to print out the process ID Description mysql is in normal operation.
According to this principle can be written as follows SHELL script:
#! / Bin / bash
check = `pgrep mysql`
if [-n "$ check"]; then
exit
else
date = $ (date + "% Y-% m-% d% H:% M:% S")
/etc/init.d/mysqld restart
echo 'mysqld exception at' $ date 'and restart'
fi
To determine whether there mysql process, the presence of the normal does not exist then the mysql may hang up, you can perform the restart operation, of course, you can also send a message directly to the specified mailbox.
2. Monitoring apache is functioning properly monitor mysql based on experience, we can write a script SHELL:
#! / Bin / bash
check = `pgrep httpd`
if [-n "$ check"]; then
exit
else
date = $ (date + "% Y-% m-% d% H:% M:% S")
/etc/init.d/httpd restart
echo 'apache exception at' $ date 'and restart'
fi
There may be a kind of apache may not be on the same server, you can use the following SHELL scripts:
if curl -m 10 -G localhost: 81> / dev / null 2> & 1
then
echo 'server is running'
else
if ping -c 1 localhost> / dev / null 2> & 1
then
echo 'server exception but ip ping success'
fi
fi
The principle is similar to directly access URL to determine whether there is a response, respectively judgment apache port and server IP. If the system is not responding to access apache service may be hung up, there is a server may be hung up, so use PING way to determine if the server is normal.
The last step is to add the script to the timing of these two tasks to perform:
crontab -e
* * * * * Sh /alidata/test/mysqlListener.sh >> /var/log/mysqlL.log
* * * * * Sh /alidata/test/httpdListener.sh >> /var/log/httpdL.log
So every minute to judge whether apache and mysql running, basically reached the real-time monitoring, if coupled with e-mail notification, as long as the system fails to achieve real-time e-mail notification. |
|
|
|