As an experienced website operation expert, I am well aware of the importance of a stable and reliable CMS system for an enterprise website.AnQiCMS with its lightweight and efficient characteristics in Go language, performs excellently in content management.However, even the most excellent system may become a major隐患 if it cannot automatically recover and run after the server restarts.start.shStart the script to ensure the system achieves boot-up automatically, so that your website service never goes down.
Ensure AnQiCMS boots up automatically on a Linux server: Deep analysisstart.shWith strategy
AnQiCMS as an enterprise-level content management system developed based on the Go language, has won the favor of many users with its high performance and ease of deployment.But in actual operation, the server may need to be restarted due to maintenance, upgrades, or other emergencies.How to ensure that AnQiCMS can start automatically and restore website services at this time is a key issue that every website operator must solve.start.sh脚本来帮助我们实现这一目标。
首先,让我们来了解AnQiCMS的start.shScript.This script is not simply a start of AnQiCMS, but a more intelligent watchdog. It will first check if there is already an instance of AnQiCMS running in the system before trying to start AnQiCMS.This 'check first, then start' mechanism greatly avoids repeated startups or problems caused by process conflicts, ensuring the robustness of the service.
#!/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
从脚本中可以看出,它定义了AnQiCMS可执行文件的名称(BINNAME)and its path(BINPATH)。The core logic is to useps -ef | grep '\<anqicms\>'to check if there is a running AnQiCMS process.existsThe variable is 0, indicating that AnQiCMS is not running, then the script will switch toBINPATHIn the directory, and throughnohupThe command starts AnQiCMS in the background and redirects its output torunning.logfile.
Understood the working principle of the script, the next step is how to configure it to start up automatically.
Method one: usecrontabImplement startup (recommended command line method)
On a Linux server,crontabIs a classic tool for managing periodic tasks, it can also meet the needs of startup self-starting. We will usecrontabfeatures, letstart.shThe script can be detected and restarted in time after the system starts, or even if AnQiCMS stops unexpectedly.
Prepare to start the script:Firstly, please make sure that your
start.shThe script is located in the AnQiCMS installation directory and already has execution permissions. If not set, you can add execution permissions using the following command:chmod +x /path/to/your/anqicms/start.shPlease replace
/path/to/your/anqicmsReplace with the actual installation path of AnQiCMS. Also, be sure to check.start.shScript insideBINPATHDoes the variable point to the correct AnQiCMS installation directory?Edit
crontabConfiguration:Enter in the terminal.crontab -eCommand, this will open the current user's scheduled task configuration file. If it is your first time using it, the system may ask you to select an editor (such asviornano).Add a scheduled task entry:At the end of the file, add the following line of content:
*/1 * * * * /path/to/your/anqicms/start.shAfter adding, save and exit the editor. For example, if you use
nano, pressCtrl+XThen pressYConfirm save, finally pressEnter. If you usevi, pressEscEnter, then input:wqthen pressEnter.Explain this line of command:
*/1 * * * *This means that the command will be executed once a minute./path/to/your/anqicms/start.shThis is your AnQiCMS.start.shthe full path of the script.
Why should it be executed once a minute?This does not mean that AnQiCMS will restart every minute, but
start.shthe script will check every minute whether AnQiCMS is running. Ifstart.shThe AnQiCMS process does not exist (for example, the system has just restarted, or the AnQiCMS process has crashed unexpectedly), it will immediately start AnQiCMS. If AnQiCMS is already running,start.shThe script will "see" it and then exit without performing any action.This mechanism ensures the high availability of AnQiCMS, even if the process is interrupted in the non-boot state, it can be automatically restored.Manually execute once for testing:In the settings
crontabThen, you can manually execute once:./start.shto verify whether the script can start AnQiCMS normally. Subsequently,ps aux | grep anqicmsCommand to check if the AnQiCMS process has started successfully.
Method two: Use the "Boot Start" feature of the Baota panel for Go projects.
For users accustomed to using Baota panel to manage servers, the deployment of AnQiCMS can be simplified even more. The 'Go project' feature of Baota panel is built-in with support for boot startup, saving the need for manual configurationcrontabThe steps.
Create a Go project in the Baota panel:Log in to the Baota panel, go to the 'Website' menu, click 'Go project', and then select 'Add Go project'.
Configure item information:
- Project executable file:Enter the executable file path of AnQiCMS, for example
/www/wwwroot/anqicms.com/anqicms. - Project Name:Enter a recognizable name, for example
AnQiCMS. - Project Port:Enter the AnQiCMS configuration file (
config.json)中设定的端口,usually set to8001. - Execute command:Enter the executable file path of AnQiCMS, for example
/www/wwwroot/anqicms.com/anqicms. - Run as user:usually choose
www. - Tick the “Start on Boot”:This is a critical step to ensure that AnQiCMS runs automatically after the server restarts.
- Domain binding:Fill in your website domain.
- Project executable file:Enter the executable file path of AnQiCMS, for example
Submit configuration:After completing the above configuration, click the 'Submit' button. Baota panel will automatically set up service guard and boot automatically for you.
No matter which method you choose, the core goal is to ensure that AnQiCMS can automatically and reliably resume operation after the server restart.crontabProvided a general and powerful Linux system-level solution, while Baota panel provides a convenient abstraction layer for users accustomed to the graphical interface.
Precautions
- Path and filename:Ensure that
start.shscript, make sure it points to the correct directory of the AnQiCMS executable file, and that the directory containsBINPATHandBINNAMEMatches the actual installation path and executable file name of AnQiCMS (default asanqicms). - Permissions:
start.shThe script and AnQiCMS executable files must have sufficient execution permissions. - Log:Pay attention to
running.logandcheck.logFiles, they record the startup and running status of AnQiCMS, which are important bases for troubleshooting. - Port occupied:When deploying across multiple sites or services, the port used by AnQiCMS (default is 8001) may be occupied by other services. You can use