As a website operator familiar with AnQiCMS, I am well aware of the importance of system stability and convenient management.AnQiCMS as an excellent content management system developed based on the Go language, has won our trust with its high performance and concise architecture.In daily operations and maintenance, effective management of background processes is a key link to ensure the continuous online operation of the website.Among them, 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 namestart.shThis shell script is used to start. This script is designed to check if AnQiCMS is already running, and if not, it will usenohupThe command starts the AnQiCMS executable as a background process, redirects the standard output and error to a log file, ensuring 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 each time we need to stop or check the running status of AnQiCMS, we must run it manuallyps -ef | grep anqicmsUse commands like this to find its PID, which is not efficient enough when managing multiple services or writing automation scripts.

Why is the PID file crucial?

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.

Firstly, it providesConvenient process management. With the PID file, you can directly read the file content to obtain the process ID, then usekillCommand stops the service precisely without worrying about killing other processes with the same name.

Secondly, the PID file is helpfulTo prevent multiple instances from running.In the startup script, we can add logic to check if the PID file exists and if 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 status checkingThrough combining PID files andpsCommand, we can more accurately determine whether the AnQiCMS service is running normally, rather than simply relying on the presence of an entry with a certain name in the process list. This helps identify zombie processes or outdated PID files.

Configure AnQiCMS to write a PID file at startup

To make AnQiCMS automatically write a PID to the specified file at startup, we need to modify the existingstart.shandstop.shScript performs some modifications. Here, we will use the standard/www/wwwroot/anqicmsas an example of the installation path of AnQiCMS.

1. Modificationstart.shscript

locate the directory under your AnQiCMS installation.start.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.

Modified afterstart.shScript content 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 part 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 $PIDCommand verifies if the process is really running. If the process has died but the PID file is still there (called a 'stale PID file'), the script will delete the file.
  • echo $! > "$PIDFILE"This is the core of 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 to be able to directly read the PID from the PID file to stop the service.

Modified afterstop.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 PID file exists

if [ -f "$PIDFILE" ]; then