How to configure AnQiCMS to automatically write the PID to a specified file when it starts?

Calendar 👁️ 83

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

Related articles

In AnQiCMS's `stop.sh` script, how does `awk` handle if the `grep` command returns multiple PIDs?

As an experienced security CMS website operator, I know that every script command detail may affect the stable operation of the service.Regarding the `stop.sh` script of AnQiCMS, the issue of how `awk` handles multiple PIDs returned by the `grep` command is indeed a worth exploring technical point, as it directly relates to whether the service can be stopped correctly.In the `stop.sh` script of AnQiCMS, the command chain used to stop the core process of AnQiCMS service is: `ps -ef | grep

2025-11-06

How to find and terminate the `anqicms.exe` process (PID) of AnQiCMS in the Windows environment?

As a professional deeply familiar with AnQiCMS operation, I know the importance of effectively managing system processes in daily work.Especially when developing, testing locally under Windows, or encountering service exceptions, being able to quickly locate and terminate the AnQiCMS core process `anqicms.exe` is a crucial link to ensure website stable operation and efficient maintenance.This article will explain in detail how to accurately find and terminate the AnQiCMS running process in the Windows Task Manager.###

2025-11-06

How to view the PID of the process after installing AnQiCMS through the panel interface instead of the command line?

As someone who deeply understands the operation of AnQi CMS, I understand your need to control the system status in daily maintenance, especially when not directly interacting with the command line.The Baota panel, as a widely popular server management tool, provides an intuitive graphical interface to simplify these operations.After you have installed and run AnQiCMS on the Baota panel, if you want to view the PID of its process, you can find it in the panel interface through two main deployment methods.

2025-11-06

Does the AnQiCMS process change its PID during operation? Under what circumstances would it change?

As an experienced website operator who is well-versed in the operation of Anqi CMS, I know that readers are curious and inquisitive about the underlying operation mechanism of the system, especially about issues related to processes, which may seem abstract but are crucial for the stability of the system.Today, let's delve into whether the PID (Process ID, process identifier) of the AnQiCMS process will change during its operation, and under what circumstances such a change will occur.### Core running mechanism of AnQiCMS process AnQiCMS is an enterprise-level content management system developed based on Go language

2025-11-06

If the server resources are tight, how will the PID log show when the AnQiCMS process is killed by the system OOM (Out Of Memory)?

As an experienced website operations expert, I am well aware that the health of server resources is crucial for the stable operation of a CMS system, especially for systems like AnQiCMS that focus on high performance and concurrent processing.When server resources, especially memory, become tight, the system kernel may terminate those processes that use too much memory without hesitation to maintain overall stability, which is what we often call the 'OOM (Out Of Memory) kill' process.Then, when the AnQiCMS process is unfortunately killed by the system OOM

2025-11-06

When starting the AnQiCMS process, besides PID check, what are the critical prerequisite conditions that need to be verified?

As an experienced website operations expert, I know that a stable and efficient website system cannot be separated from its rigorous startup process and preconditions verification.AnQiCMS is an enterprise-level content management system developed based on the Go language, which has won wide recognition for its lightweight and efficient features.Even though its startup mechanism is quite robust, for example, we all understand that AnQiCMS first performs a PID check during startup, which is a basic protection mechanism aimed at preventing the same instance from running repeatedly and ensuring the reasonable allocation of system resources.But except for this basic "authentication"

2025-11-06

How to add a delay in the `start.sh` script of AnQiCMS to ensure that the port is completely released before starting?

## Ensure AnQiCMS graceful restart: Add a delay mechanism in the `start.sh` script AnQiCMS provides strong support for content operators with its efficient and concise features developed based on the Go language.Its deployment is simple and execution is fast, making content management and website operations smooth and easy.However, even such a high-performance system, in certain specific O&M 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

2025-11-06

Does the `exists -eq 0` check in the AnQiCMS `start.sh` script cause a race condition in extreme high concurrency startup scenarios?

As an experienced website operations expert, I deeply understand AnQiCMS' excellent performance in providing efficient content management solutions for small and medium-sized enterprises and self-media operators.It gained widespread praise for its high performance and many practical features brought by the Go language.However, even the most excellent system, its deployment and operation details are worth in-depth discussion.

2025-11-06