How to configure AnQiCMS to automatically write the PID to a specified file when it starts?
As a website operator familiar with AnQiCMS, I am well aware of the importance of system stability and convenient management.AnQiCMS is an excellent content management system developed based on the Go language, which has won our trust with its high performance and simple architecture.In daily operations and maintenance, effective management of background processes is the key to ensuring that the website remains online.The use of the process ID (PID) file can greatly simplify the operations of starting, stopping, and checking the status of AnQiCMS services.
Understanding the startup mechanism of AnQiCMS
According to the AnQiCMS documentation, the system usually uses a name calledstart.shA shell script to start. This script is designed to check if AnQiCMS is already running, if not, it will usenohupThe command starts the executable file of AnQiCMS as a background process, redirects standard output and error to the log file, ensuring that the program continues to run even after the terminal is closed.
originalstart.shThe script is roughly as follows:
#!/bin/bash
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms # 请根据实际路径修改
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |wc -l`
if [ $exists -eq 0 ]; then
cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
fi
Although this method can effectively start the service, it does not write the PID of the background process to a fixed file. This means that every time we need to stop or check the running status of AnQiCMS, we need to run it manuallyps -ef | grep anqicmsUse commands to find its PID, which is not very efficient when managing multiple services or writing automation scripts.
Why is the PID file crucial
The PID file (Process ID file) is a text file containing a unique identifier for a single running process (i.e., the process ID).For any application that needs to run as a background service (daemon), maintaining a PID file is almost an industry standard practice.Its main advantages are reflected in the following aspects:
Firstly, it providesConvenient process management. With the PID file, you can directly read the file content to obtain the process ID and then usekillthe command to stop the service accurately without worrying about mistakenly killing other processes with the same name.
Secondly, the PID file is helpful forpreventing multiple instance runsIn the startup script, we can add logic to check if the PID file exists and whether the process recorded in it is still running.This can effectively avoid accidentally starting multiple instances of AnQiCMS, which may cause resource conflicts or unexpected behavior.
Finally, it can achievemore precise process state checks. By combining the PID file withpsCommand, we can more accurately determine whether the AnQiCMS service is running normally, rather than simply relying on whether there is an entry with a certain name in the process list. This helps to identify zombie processes or outdated PID files.
Configure AnQiCMS to write PID file at startup
In order to make AnQiCMS automatically write PID to the specified file at startup, we need to modify the originalstart.shandstop.shThe script makes some modifications. Here, we will use the standard/www/wwwroot/anqicmsas an example of the installation path for AnQiCMS.
1. Modifystart.shscript
Locate the AnQiCMS installation directory under yourstart.shFile, and open it with a text editor. We will introduce aPIDFILEvariable to specify the path of the PID file, and capture the PID of the newly started process after the startup command and write it to the file.
Modifiedstart.shThe script content is as follows:
#!/bin/bash
### check and start AnqiCMS
# author fesion
# the bin name is anqicms
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms # 请根据实际路径修改 AnQiCMS 的安装目录
PIDFILE="$BINPATH/$BINNAME.pid" # 定义 PID 文件路径
# 检查是否存在 PID 文件,如果存在则判断进程是否仍在运行
if [ -f "$PIDFILE" ]; then
PID=$(cat "$PIDFILE")
if ps -p $PID > /dev/null; then
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME is already running with PID $PID"
exit 0 # 进程已运行,退出
else
echo "$(date +'%Y%m%d %H:%M:%S') Stale PID file found, removing $PIDFILE"
rm -f "$PIDFILE" # 进程不存在,删除旧的 PID 文件
fi
fi
# 启动 AnQiCMS
echo "$(date +'%Y%m%d %H:%M:%S') Starting $BINNAME..."
cd "$BINPATH" && nohup "$BINPATH/$BINNAME" >> "$BINPATH/running.log" 2>&1 &
echo $! > "$PIDFILE" # 捕获后台进程的PID并写入文件
if [ -f "$PIDFILE" ]; then
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME started with PID $(cat $PIDFILE)"
else
echo "$(date +'%Y%m%d %H:%M:%S') Failed to create PID file or start $BINNAME"
fi
Key modification description:
PIDFILE="$BINPATH/$BINNAME.pid": Defines the complete path of the PID file, for example/www/wwwroot/anqicms/anqicms.pid.if [ -f "$PIDFILE" ] ... fiThis logic enhances the robustness of the startup script. It first checks if the PID file exists, and if it does, it reads the PID from it and usesps -p $PIDThe command verifies whether the process is really running. If the process has died but the PID file is still there (known as a 'stale PID file'), the script will delete the file.echo $! > "$PIDFILE"This is the core.$!It is a special shell variable that stores the PID of the last command run in the background (i.e.)nohup ... &). We redirect it and write it to$PIDFILE.
2. Modifystop.shscript
To accommodate the new PID file mechanism,stop.shthe script also needs to be updated so that it can directly read the PID from the PID file to stop the service.
Modifiedstop.shScript content as follows:
“`bash
#!/bin/bash
stop anqicms
author fesion
the bin name is anqicms
BINNAME=anqicms BINPATH=/www/wwwroot/anqicms # Please modify the installation directory of AnQiCMS according to the actual path
PIDFILE=(BINPATH/)BINNAME.pid" # Define PID file path"}
Check if the PID file exists
if [ -f “$PIDFILE” ]; then