On a Linux server, how can the AnQiCMS startup script `start.sh` ensure boot-up?
As an experienced website operations expert, I know the importance of a stable and reliable CMS system for a corporate website.AnQiCMS with its lightweight and efficient features in Go language performs excellently in content management.However, even an excellent system may pose a major隐患 in operation if it cannot automatically resume operation after the server restarts.Today, let's delve into how to use the built-in AnQiCMS on a Linux server,start.shStart the script to ensure the system implements boot-up and keeps your website service online without any downtime.
Ensure AnQiCMS boots up on Linux servers: In-depth analysisstart.shWith strategy
AnQiCMS is an enterprise-level content management system developed based on the Go language, which 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 emergency situations.How to ensure that AnQiCMS can automatically start and restore website services at this time is a key issue that every website operator must solve.Fortunately, AnQiCMS provides a well-designedstart.shScripts to help us achieve this goal.
First, let's understand the AnQiCMS.start.shScript. This script is not just a simple way to start AnQiCMS, but also a more intelligent guard process, which will first check if there is already an instance of AnQiCMS running in the system before attempting 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
From the script, it can be seen that it defines the name of the AnQiCMS executable file (BINNAME)and its path(BINPATH)。The core logic is to useps -ef | grep '\<anqicms\>'to check if there is a running AnQiCMS process. IfexistsVariable is 0, indicating that AnQiCMS is not running, the script will switch toBINPATHIn the directory and throughnohupCommand runs 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: utilizecrontabImplement startup automatic (recommended command line method)
On a Linux server,crontabIt is a classic tool for managing periodic tasks, and it can also meet the needs of boot-up startup. We will utilizecrontabits characteristics, to makestart.shThe script can be detected and restarted in time after the system starts up, or even if AnQiCMS stops unexpectedly.
Prepare to start the script:First, make sure you have 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, make sure to check.start.shwithin the scriptBINPATHIs the variable pointing 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 you are using it for the first time, the system may ask you to select an editor (such asviornano)Add a scheduled task entry:Add the following line at the end of the file:
*/1 * * * * /path/to/your/anqicms/start.shAfter adding, save and exit the editor. For example, if you are using
nanoPressCtrl+XThen pressYConfirm save and finally pressEnter. If you useviPressEsc, then enter:wqand sort byEnter.To explain this command:
*/1 * * * *: This means the command will execute once a minute./path/to/your/anqicms/start.sh: This is your AnQiCMS'sstart.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 to see if AnQiCMS is running. Ifstart.shDetected that the 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 doing anything.This mechanism ensures the high availability of AnQiCMS, even if the process is interrupted during non-boot status, it can automatically recover.Perform a manual execution for testing:In the setting
crontabAfter that, you can manually execute once:./start.shTo verify if the script can start AnQiCMS normally. Then, throughps aux | grep anqicmsCheck if the AnQiCMS process has successfully started.
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. The 'Go project' function of Baota Panel is built-in with support for boot-up startup, eliminating the need for manual configurationcrontabsteps.
Create a Go project on Baota panel:Log in to Baota panel, go to the “Website” menu, click “Go project”, and then select “Add Go project”.
Configure project information:
- Project executable file:Enter the executable file path of AnQiCMS, for example
/www/wwwroot/anqicms.com/anqicms. - Project name:Enter a name that is easy to identify, for example
AnQiCMS. - Project port:Enter the AnQiCMS configuration file (
config.jsonThe port set, usually the default one8001. - Execute the command:Enter the executable file path of AnQiCMS, for example
/www/wwwroot/anqicms.com/anqicms. - Run user:Generally selected
www. - Check the "Boot-up" option:This is a critical step to ensure that AnQiCMS runs automatically after the server restarts.
- Bind domain:Enter your website domain name.
- Project executable file:Enter the executable file path of AnQiCMS, for example
Submit configuration:After completing the above configuration, click the "Submit" button. The Baota panel will automatically set up service protection and boot automatically for you.
No matter which way you choose, the core goal is to ensure that AnQiCMS can automatically and reliably resume operation after the server restarts.crontabProvided a general and powerful Linux system-level solution, while Baota panel provides a convenient abstraction layer for users accustomed to the graphical interface.
Points to note
- Path and filename:Make sure to
start.shin the scriptBINPATHandBINNAMEmatches the actual installation path and executable file name of AnQiCMS (default asanqicms). - Permissions:
start.shThe script and AnQiCMS executable files need sufficient execution permissions. - Log:Pay attention.
running.logandcheck.logFiles that record the startup and running status of AnQiCMS are important for troubleshooting. - Port occupied:When deploying on multiple sites or services, the port used by AnQiCMS (default is 8001) may be occupied by other services. You can use