Ensure AnQiCMS graceful restart: instart.shAdd delay mechanism in script
AnQiCMS with its efficient and concise features based on the Go language provides strong support for content operators.Its deployment is simple, with fast execution speed, making content management and website operation effortless.However, even such a high-performance system, in certain specific operation and maintenance scenarios, such as frequent restarts, updates and deployments, or when the server resources are tight, you may accidentally encounter a pesky problem: the program fails to start and prompts 'Address already in use' (the address is already in use).
This is not usually a defect of AnQiCMS itself, but a common phenomenon in the handling of process termination and port release in Linux/Unix systems. When a program is stopped (especially throughkill -9This forced method), the operating system will not immediately release all the resources it occupies, including network ports. These ports may enterTIME_WAITStatus, it takes a while before it is fully available.If your startup script attempts to start a new process immediately after the old process is killed, the new process may fail to start due to the port still being occupied.
To ensure that AnQiCMS can start smoothly and elegantly every time, we canstart.shAdd a simple delay mechanism to the startup script.This is like giving the system some "breathing room" time, allowing it to thoroughly clean up the ports left behind by old processes in preparation for new ones.
Understandingstart.shThe working principle of the script
During the deployment of AnQiCMS,start.sh脚本扮演着一个重要的角色。它通常被配置为计划任务(English)crontab),负责定期检查 AnQiCMS 进程是否正在运行。如果发现进程不在,它就会启动 AnQiCMS。让我们回顾一下典型的 Englishstart.shScript core logic:
#!/bin/bash
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms # 您的实际路径可能不同
# 检查 anqicms 进程是否存在
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
if [ $exists -eq 0 ]; then
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME NOT running, attempting to start..." >> $BINPATH/check.log
# 在这里,AnQiCMS 进程将会被启动
cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME started." >> $BINPATH/check.log
else
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME is already running." >> $BINPATH/check.log
fi
You can see that the script first usesps -ef | grepcommands to judgeanqicmsif the process exists. IfexistsVariable is0(indicating the process does not exist), it willcdGo to the specified directory and usenohupCommand to start in the backgroundanqicmsExecutable file.
Insert delay to solve port occupation problem
The root cause of the problem is, ifanqicmsProcess has just beenstop.shkilled by script or manual command, andstart.shtriggered and detected immediatelyexistsresponse for0, so in the case where the old port has not been fully released, the newanqicmsThe process will attempt to bind the same port, resulting in failure to start.
To avoid this situation, we can add a simplestart.shcommand before the script starts.sleepa command.sleepThe command pauses the script execution for a specified number of seconds, providing the system with necessary buffer time.
The following is the modifiedstart.shThe script, the key change issleep 5this line:
#!/bin/bash
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms # 您的实际路径可能不同
# 检查 anqicms 进程是否存在
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
if [ $exists -eq 0 ]; then
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME NOT running, attempting to start..." >> $BINPATH/check.log
# 新增:加入延时,等待端口完全释放
sleep 5
cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME started." >> $BINPATH/check.log
else
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME is already running." >> $BINPATH/check.log
fi
Operation Steps:
- Find
start.shfile:It is usually located in your AnQiCMS deployment directory (for example/www/wwwroot/anqicms/start.sh). - Edit file:Use
vi/nanoor open the file manager of Baota panelstart.sh. - Insert
sleep 5:Inif [ $exists -eq 0 ]; theninside the code block,紧接着echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME NOT running, attempting to start..."the line after this,nohupBefore the actual startup command, insertsleep 5. - Save and exit.
With this tiny change, you have won several precious seconds for AnQiCMS to clean up the environment. Whenstart.shIt will pause for 5 seconds before attempting to start AnQiCMS when the process needs to be started, which greatly reduces the probability of failure due to port occupation.
Thought and **Practice
- Duration extended:
sleep 5Provided a reasonable starting value. In most cases, 5 seconds is enough for the operating system to release the port