In website operation, service stability and data integrity are core elements.It is particularly important for an efficient content management system like AnQiCMS, which is developed based on Go language, to shut down gracefully rather than be forcefully closed.This not only concerns the standardization of technology, but also directly affects the normal operation of the website, user experience, and the security of key data.
Why is graceful shutdown so important?
Imagine that you are busy editing an important article, or the user is browsing your product page.What might happen if the AnQiCMS process on the server is abruptly interrupted at this time?
- Data loss or corruption:The ongoing database write operation may not have been completed, resulting in inconsistent data or even damage.
- User experience interruption:Users visiting your website may encounter error pages or services being unavailable.
- Resource could not be released:Database connections, file locks, and other resources may not have been closed properly, affecting the start of subsequent services or causing resource leaks.
- Incomplete log recording:Important operation logs may not have been written in time, which may cause difficulties in subsequent fault troubleshooting.
Therefore, stopping AnQiCMS gracefully means that the system has enough time to complete the current tasks, save all unsaved data, release occupied resources, and send a clear exit signal to the operating system, thereby avoiding the above potential problems.
The operation mechanism and shutdown command of AnQiCMS
AnQiCMS as a Go language application usually runs as an independent process on the server and provides services to the outside world through reverse proxies such as Nginx and Apache.Its start and stop often depend on scripts or container management tools.
According to the documentation provided by AnQiCMS, in a Linux environment, we usually usestart.shandstop.shScripts like this manage their lifecycle by default.start.shThe script will checknohup ... &The command starts the AnQiCMS process in the background and ensures it continues to run after the terminal closes.
# start.sh 脚本示例
#!/bin/bash
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |wc -l`
if [ $exists -eq 0 ]; then
echo "$BINNAME NOT running"
cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
fi
However, it is worth noting that the official documentation providesstop.shthe script example useskill -9this mandatory shutdown command:
# stop.sh 脚本示例 (官方文档提供)
#!/bin/bash
BINNAME=anqicms
BINPATH="$( cd "$( dirname "$0" )" && pwd )"
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |awk '{printf $2}'`
if [ $exists -eq 0 ]; then
echo "$BINNAME NOT running"
else
echo "$BINNAME is running"
kill -9 $exists # 这里是问题所在
echo "$BINNAME is stop"
fi
kill -9(i.e.),SIGKILLA signal is an 'immediate termination' command that forces the operating system to immediately stop the target process, giving the process no opportunity to clean up or save data.This is like suddenly cutting off the power, rather than through the normal shutdown process.Even though the process is unresponsivekill -9It is a last resort, but it is not an elegant choice for a healthy, responsive application.
Achieve an elegant shutdown of AnQiCMS
Applications written in Go language usually can respondSIGTERMsignal (i.e.killThe default signal sent by the command, thus executing cleanup logic and exiting gracefully.AnQiCMS's update log mentioned 'graceful startup and restart of the blog', which implies that the system has the ability to handle such signals.
In order to make AnQiCMS stop gracefully, we should modifystop.shthe script, andkill -9changed tokill:
Modifiedstop.shscript example
#!/bin/bash
### stop anqicms gracefully
# author fesion (modified for graceful shutdown)
# the bin name is anqicms
BINNAME=anqicms
BINPATH="$( cd "$( dirname "$0" )" && pwd )"
# 查找AnQiCMS进程的PID
PID=$(ps -ef | grep '\<'$BINNAME'\>' | grep -v grep | awk '{print $2}')
if [ -z "$PID" ]; then
echo "$BINNAME NOT running"
else
echo "$BINNAME is running with PID $PID"
echo "Attempting graceful shutdown for $BINNAME..."
kill "$PID" # 发送SIGTERM信号,允许进程自行清理
# 等待进程退出,给AnQiCMS留出清理时间,例如10秒
COUNT=0
while kill -0 "$PID" 2>/dev/null; do
if [ "$COUNT" -ge 10 ]; then # 如果10秒后进程仍未退出,则强制终止
echo "Graceful shutdown failed, forcing $BINNAME to stop..."
kill -9 "$PID"
break
fi
sleep 1
COUNT=$((COUNT+1))
done
echo "$BINNAME has stopped."
fi
Key modification point:
kill "$PID"This will send to the AnQiCMS processSIGTERMThe signal. If AnQiCMS handles this signal correctly, it will perform operations such as closing the database connection, completing the current request, saving session data, and then exit by itself.- Increase wait and timeout mechanism:To ensure the process has time to clean up, we added a
whileloop to wait for the process to exit.kill -0 "$PID"Can check if the process exists but will not send the actual signal. If AnQiCMS does not exit gracefully within the set time (such as 10 seconds), we will usekill -9Terminate forcibly to prevent process from becoming a zombie.
Graceful shutdown under Docker environment
If you run AnQiCMS in a Docker environment (such as 1Panel, aaPanel, or manual Docker deployment), the situation is even simpler. Docker'sdocker stopThe command is sent to the main process of the container by defaultSIGTERMsignal (i.e.killCommand), and wait for a period of time (default is 10 seconds) for the application to exit on its own. If the application does not exit within this time, Docker will sendSIGKILLsignal (i.e.kill -9Force termination.
This means that if your AnQiCMS Docker container is properly configured and the AnQiCMS application itself can respondSIGTERMsignal, then throughdocker stop [容器名或ID]To stop the service has already become a graceful way to stop.
Precautions under Windows/MacOS environment
On desktop operating systems such as Windows or MacOS, manually "end task" or "force quit" applications through the Task Manager or Activity Monitor essentially involveskill -9Similar, all are forced to terminate. Although the Go program can also respond to some signals on these systems, graphical interface operations usually do not provide an option for "graceful shutdown" unless the application itself provides an internal exit button or API.For AnQiCMS, the risk of forced shutdown is relatively low when used as a test or development environment on a desktop system, but it is strongly recommended to use script management or Docker management under Linux in the production environment.
Summary
Gracefully stopping AnQiCMS is a good operational practice, which can effectively protect data, enhance system stability and optimize user experience. By usingstop.shin the scriptkill -9ReplacekillWith an appropriate timeout mechanism, we can ensure that AnQiCMS performs necessary cleanup before exiting. In the Docker environment,docker stopThe command itself provides this elegant stop process. As a website operations expert, adopting these strategies will make your AnQiCMS project management more professional and reliable.
Frequently Asked Questions (FAQ)
Q1: Why do we saykill -9It is forced shutdown, not graceful shutdown? What are the hazards?
A1:kill -9The one sent isSIGKILLA signal, this is a mandatory command at the operating system level, the process cannot capture or respond to this signal. This means that it iskill -9The process will stop immediately, with no opportunity to perform cleanup operations, such as saving cached data, closing file handles, releasing network connections, and synchronizing database status, etc.This could lead to data loss, file corruption, resource leakage, and incomplete or inconsistent system states, which is a high-risk operation in a production environment.
**Q2: If I modifiedstop.shThe script iskillBut what if AnQiCMS still does not exit gracefully and keeps running in a deadlock state?