How to automatically restore the service after the AnQiCMS server restarts using `crontab`?
As an experienced website operations expert, I know that the stable operation of the website is the foundation for the continuous exertion of content value.For systems like AnQiCMS that are committed to providing efficient and reliable content management solutions, ensuring the continuous online service is of great importance.Unexpected server restart, whether it is planned maintenance or an emergency situation, it may lead to the interruption of AnQiCMS services, thereby affecting user access and content presentation.Therefore, mastering how to automatically restore the AnQiCMS service after a server restart is a skill that every operator must have.Today, let's delve into how to use the built-in time task scheduling tool of the Linux systemcrontabTo achieve this goal.
Why is the automatic recovery service so important?
AnQiCMS as an enterprise-level CMS developed based on the Go programming language, with its high-performance architecture, high concurrency features, and SEO-friendly design, all aim to provide users with stable and efficient content services.Whether it is content marketing for small and medium-sized enterprises, self-media publishing of the latest information, or multi-site management, the continuous availability of AnQiCMS is directly related to the website's traffic, user experience, and even brand reputation.Manually intervening to restart the service not only takes time, but also may cause a long service interruption if problems occur during off-hours. BycrontabImplement service auto-recovery to minimize downtime to the greatest extent possible, ensuring that the "lightweight and efficient" concept of AnQiCMS is always throughout the entire life cycle of the website.This has improved the operational efficiency, also indirectly ensuring the website's SEO performance and user retention, in line with the design principles of the AnQiCMS project 'paying attention to high concurrency, security, and scalability.'
The startup and shutdown mechanism of AnQiCMS services
Starting the configurationcrontabPreviously, we first need to understand how AnQiCMS starts and stops. According to the AnQiCMS installation documentstart.md),The project has prepared a dedicated automatic script for the production environment:start.shfor starting,stop.shUsed to stop. These scripts are located in the AnQiCMS installation directory, they encapsulate the complex commands required to start the Go program, and add process checking logic, making service management more convenient.
Start withstart.shFor example, its core logic is as follows:
#!/bin/bash
### check and start AnqiCMS
# author fesion
# the bin name is anqicms
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms # 这个路径需要根据你的实际安装路径修改
# check the pid if exists
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |wc -l`
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME PID check: $exists" >> $BINPATH/check.log
echo "PID $BINNAME check: $exists"
if [ $exists -eq 0 ]; then
echo "$BINNAME NOT running"
cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
fi
This script is very intelligent, and it will first check the name ofanqicmsWhether the process is already running. If it is not running(exists -eq 0) it will executenohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &Command to start the service, and redirect the output torunning.logFile, and run the process in the background(nohup ... &)。If the service is already running, the script will not perform any operation. This 'check before start' mechanism is exactly what we are taking advantage ofcrontabto achieve service self-healing.
UtilizecrontabImplement automatic recovery service
crontabIt is a time-based task scheduler tool that comes with the Linux system, which can be used to execute commands or scripts at specified times or frequencies. Combined with AnQiCMS'sstart.shScript, we can easily configure a scheduled task to have the system check the AnQiCMS service status at regular intervals (such as every minute) and automatically start it if the service is not running.
The following are the detailed steps:
1. Confirm the installation path and script executability of AnQiCMS
Before executing anycrontabBefore configuring, please make sure to confirm the installation directory of AnQiCMS, especiallyanqicmsand the executable filestart.shThe accurate location of the script. Assuming your AnQiCMS is installed in/www/wwwroot/anqicmsdirectory, then:
- Ensure
start.shThe script has execution permissions. If the file does not have execution permissions, you can add it using the following command:chmod +x /www/wwwroot/anqicms/start.sh - Run manually once
start.shTo verify that it works correctly:
If you seecd /www/wwwroot/anqicms ./start.sh # 稍等片刻,检查AnQiCMS进程是否启动 ps -ef | grep anqicmsanqicmsThe process is running, which means the script is valid. If the service is already running, you can stop the service first through./stop.shand then test it../start.sh.
2. Edit Crontab Task List
Enter the following command in your server terminal to edit the current user'scrontabTask List:
crontab -e
If it is the first time you are usingcrontab -eThe system may ask you to select a text editor (for examplevi/nanoetc.). Choose the editor you are familiar with.
3. AddcrontabAutomatically restore task
In the open editor, add a new task entry at the end of the file. It is recommended to add the following content:
# 每分钟检查并启动 AnQiCMS 服务
*/1 * * * * /www/wwwroot/anqicms/start.sh
The meaning of this line of tasks is:
*/1 * * * *This iscrontabThe time expression, indicating "execute at the 0th second of every minute".- The first
*/1: minute (every 1 minute) - the second
*: hour (every hour) - the third
*: date (every day) - The fourth
*: month (every month) - Fifth
*Weekday (any day of the week)
- The first
/www/wwwroot/anqicms/start.sh: This is the full path of the command to be executed. Please make sure to replace it with the actual installation directory of your AnQiCMS.start.shScript path.
As mentioned earlier,start.shThe script itself checks if AnQiCMS is running. Therefore, evencrontabEvery minute, this script is executed and it does not restart the service repeatedly; it only ensures that the service is restarted when it is interrupted. This is a very robust self-healing strategy.
4. Save and Exit Editor
- If you are using
viEditor: PressEsckey, then enter:wqand press Enter. - If you are using
nanoEditor: PressCtrl + XThen pressYkey to confirm save, finally press Enter.
5. VerifycrontabHas the task been added
After saving and exiting, you can enter the following command again to view yourcrontabtask list, to confirm whether the newly added entry has taken effect:
crontab -l
You should be able to see the task you just added.*/1 * * * * /www/wwwroot/anqicms/start.shThis line.
6. Testing automatic recovery function
In order to verify that the configuration is successful, you can try to manually stop the AnQiCMS service, then wait for one or two minutes to see if the service automatically recovers