Business background:
There are often businesses in the web that need to be executed regularly. However, in a cluster environment, if scheduled tasks in multiple servers are started at the same time, the database will hang.
It should actually work. Only one scheduled task accesses the database.
How to solve this problem:
1. In a multi-server cluster, the directory is mounted. Only one server stores scheduled tasks. When mounting, exclude the configuration file of scheduled tasks and only allow one server to enable scheduled tasks.
2. Deploy a separate server to run scheduled tasks
3. Use a Linux script to access the cluster server page or action. There must be a server that receives the request and then executes the scheduled task.
What I want to talk about here is the third method. The disadvantage is that the pages or actions of the accessed server need to be subject to security restrictions.
First, configure the linux scheduled task:
vi /etc/crondtab
<p class="sycode"> <p class="sycode"> 0 1 * * * root /dbdata/backup/cms/async.sh </p> </p>
Then in async.sh, if there is an error in the request, send an email to alert:
<p class="sycode"> <p class="sycode"> #!/bin/bash </p> <p class="sycode"> Dir="/home/webtrn/CrontabDir" </p> <p class="sycode"> echo `date +"%Y-%m-%d-%H-%M"` >> $Dir/checklearntime.log </p> <p class="sycode"> wget -t1 http://www.baidu.com/checkserver/XXX.jsp -o $Dir/wgetchecklearntime.log </p> <p class="sycode"> grep -q "404" $Dir/wgetchecklearntime.log || grep -q "302" $Dir/wgetchecklearntime.log </p> <p class="sycode"> if [ $? != 0 ]; then </p> <p class="sycode"> echo "checklearntime have done" >> $Dir/checklearntime.log </p> <p class="sycode"> else </p> <p class="sycode"> echo "checklearntime is error" >> $Dir/checklearntime.log </p> <p class="sycode"> echo "checklearntime is error" | mail -s "webtrn" zhugaojian@whaty.com </p> <p class="sycode"> echo "checklearntime is error" | mail -s "webtrn" jinqingwen@whaty.com </p> <p class="sycode"> fi </p> <p class="sycode"> rm -f XXX.jsp </p> <p class="sycode"> rm -f $Dir/wgetchecklearntime.log </p> </p>