As a senior website operations expert, I know that the stable operation of the website is the cornerstone for the continuous exertion of content value.For a system like AnQiCMS that is dedicated to providing efficient and reliable content management solutions, ensuring the continuous online service is of paramount importance.An unexpected restart of the server, whether it is planned maintenance or an emergency situation, may cause the AnQiCMS service to be interrupted, thereby affecting user access and content presentation.Therefore, mastering how to automatically restore the AnQiCMS service after a server restart is an essential skill for every operator.crontabTo achieve this goal.
Why is it so important to automatically restore the service?
AnQiCMS, as an enterprise-level CMS developed based on the Go language, boasts a high-performance architecture, high concurrency features, and a SEO-friendly design, all aimed at providing users with stable and efficient content services.Whether it is content marketing for small and medium-sized enterprises, self-media release of the latest information, or multi-site management, the continuous availability of AnQiCMS is directly related to the website traffic, user experience, and even brand reputation.Manually intervening to restart the service not only takes time, but may also lead to a long service interruption if problems occur during off-hours.crontabImplement the automatic recovery of services, to minimize downtime to the maximum extent, ensuring that AnQiCMS's 'lightweight and efficient' philosophy is always贯穿 throughout the entire lifecycle of the website.This not only improves operational efficiency, but also indirectly ensures the SEO performance and user retention of the website, in line with the design philosophy of the AnQiCMS project 'focusing on high concurrency, security, and scalability'.
The startup and shutdown mechanism of AnQiCMS
Before starting the configurationcrontabBefore, we first need to understand how AnQiCMS starts and stops. According to the AnQiCMS installation document (start.md),The project has prepared a special automatic script for the production environment:.start.shto start,stop.shStop.These scripts are located in the installation directory of AnQiCMS, they encapsulate the complex commands required to start Go programs, and add logic such as process checking, making service management more convenient.
Bystart.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, it will first check for a script namedanqicmsIs the process already running. If not running(exists -eq 0), it will execute.nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &Use the 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 action. This 'check before start' mechanism is exactly what we usecrontabto achieve service self-healing.
UtilizecrontabImplement automatic recovery service
crontabIs a time-based task scheduler tool bundled with Linux systems, which can be used to execute commands or scripts at specified times or at specified frequencies. Combined with AnQiCMSstart.shScript, we can easily configure a scheduled task to let 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 operation steps:
1. Confirm the installation path of AnQiCMS and the executability of the script
Before executing anycrontabPlease confirm the installation directory of AnQiCMS before configuration, especiallyanqicmsthe location of the executable files andstart.shscripts. Assuming your AnQiCMS is installed in/www/wwwroot/anqicmsIn the directory, 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.shRun it manually once
start.shTo verify that it works correctly:cd /www/wwwroot/anqicms ./start.sh # 稍等片刻,检查AnQiCMS进程是否启动 ps -ef | grep anqicmsIf you see
anqicmsThe process is running, indicating that the script is valid. If the service is already running, you can first./stop.shstop the service and then test./start.sh.
2. Edit the Crontab task list
Enter the following command in your server terminal to edit the current user'scrontabTask list:
crontab -e
If it's the first time you are usingcrontab -ethe system may ask you to choose a text editor (such asvi/nano[en] Select your familiar editor.) Choose the one you are familiar with.
[en] 3. Addcrontab[en] Automatically resume task
[en] In the opened 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
This line of task means:
*/1 * * * *: Thiscrontabis a time expression, indicating that the task is executed at the 0th second of every minute.- first
*/1: minute (every 1 minute) - second
*: hours (per hour) - third
*: date (every day) - fourth
*Month (each month) - Fifth
*: Day of the week (any day of the week)
- first
/www/wwwroot/anqicms/start.shThis is the complete 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 will check if AnQiCMS is already running. Therefore, evencrontabThis script is executed every minute without restarting the service repeatedly, ensuring that it is restarted only when the service is interrupted. This is a very robust self-healing strategy.
4. Save and exit the editor
- If you are using
viEditor: PressEsckey, then enter:wqand press Enter. - If you are using
nanoEditor: PressCtrl + XThen pressYConfirm and save, then press the Enter key.
5. VerificationcrontabIf the task has been added
After saving and exiting, you can enter the following command again to view yourcrontabTask list, confirm if the newly added item has taken effect:
crontab -l
You should see the one just added*/1 * * * * /www/wwwroot/anqicms/start.shon this line.
6. Test the auto-recovery function
To verify if 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 recovers automatically