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

Calendar 👁️ 72

Ensure AnQiCMS graceful restart: instart.shAdd delay mechanism in the script

AnQiCMS with its efficient and concise features developed based on the Go language provides strong support for content operators.It is simple to deploy and fast to execute, making content management and website operation very skillful.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 of Linux/Unix systems in handling process termination and port release. 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_WAITThe status will be available after a period of time. If your startup script tries to start a new process immediately after the old process is killed, the new process may fail to start because the port is still occupied.

To ensure that AnQiCMS can start smoothly and elegantly every time, we can do it in itsstart.shAdd a simple delay mechanism to the startup script. This is like giving the system a moment to breathe, allowing it to thoroughly clean up the ports left by old processes, and prepare for new processes.

Understandingstart.shThe working principle of the script

During the deployment of AnQiCMS,start.shThe script plays an important role. It is usually configured as a scheduled task (crontabResponsible for regularly checking if the AnQiCMS process is running. If it finds that the process is not running, it will start AnQiCMS. Let's review a typicalstart.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

As you can see, the script first usesps -ef | grepcommands to judgeanqicmswhether the process exists. IfexistsVariable is0(indicating that the process does not exist), it willcdGo to the specified directory and usenohupcommand to start in the backgroundanqicmsExecutable file.

Insert a delay to solve the port occupation problem

The crux of the problem lies in ifanqicmsThe process has just beenstop.shKilled by script or manual command, andstart.shIt is triggered and detected immediatelyexistsWith0Then, in the case that the old port has not been fully released, the newanqicmsprocess will try to bind to the same port, resulting in a failure to start.

To avoid this situation, we canstart.shadd a simple command before the script startssleepcommand.sleepThe command will pause the script's execution for the specified number of seconds, providing necessary buffer time for the system.

The following has been modifiedstart.shScript, the key change is insleep 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:

  1. Findstart.shFile:Generally located in your AnQiCMS deployment directory (for example/www/wwwroot/anqicms/start.sh)
  2. Edit file:Usevi/nanoor open the file manager on Baota panelstart.sh.
  3. Insertsleep 5:Inif [ $exists -eq 0 ]; thenInside the code block,紧接着echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME NOT running, attempting to start..."After this line,nohupInsert before the actual command starts,sleep 5.
  4. Save and exit.

With this small change, you have gained several precious seconds for AnQiCMS to clean up the environment. Whenstart.shDetected that the process needs to start, it will pause for 5 seconds first, and then try to start AnQiCMS, which greatly reduces the probability of failure to start due to port occupation.

Think 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

Related articles

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

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

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 as an excellent content management system developed based on the Go language, 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 greatly simplifies the operations of starting, stopping, and checking the status of AnQiCMS services.###

2025-11-06

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

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

How to quickly view the root directory path of the current AnQiCMS installation?

As an experienced website operations expert, I am well aware that clearly understanding the file structure and critical path of AnQiCMS website is of great importance when managing and maintaining it.Especially when adjusting configurations, modifying templates, troubleshooting, or managing multiple sites, quickly locating the root directory path of AnQiCMS can greatly enhance our work efficiency.AnQiCMS is a modern content management system developed based on the Go language, with flexible and diverse deployment methods, ranging from running directly on the server to managing through panels such as Baota, 1Panel, and containerized deployment

2025-11-06

Where is the default directory for storing AnQiCMS template files?

## Unveiling the Location of AnQiCMS Template Files: Default Directory Depth Analysis When building and operating a website with AnQiCMS, template files are the core that determines the appearance and interaction of the website.Whether it is to carry out personalized customization, fault diagnosis, or secondary development, clearly understanding the storage location of the template files is the first step.Where is the default template file stored for AnQiCMS?The answer is the `/template` directory. ###

2025-11-06

Where should the static resources such as CSS, JS, and images used by the website front-end be placed?

As an experienced website operations expert, I am well aware that the reasonable management of website front-end static resources is crucial for website performance, maintenance convenience, and even search engine optimization (SEO).AnQiCMS (AnQiCMS) took this into full consideration from the very beginning of the system design, providing developers and operators with a clear and efficient path for resource management.Today, let's delve into the discussion of where to place the CSS, JS, and image static resources used on your website's front end in AnQiCMS, in order to achieve the most reasonable and efficient practice.

2025-11-06