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

Calendar 👁️ 62

As an experienced AnQi CMS website operation personnel, I am well aware that every script command detail may affect the stable operation of the service. For AnQiCMS'sstop.shScript, which is aboutgrepWhen the command returns multiple PIDs,awkHow to deal with the issue, it is indeed a technical point worth further exploration, as it directly relates to whether the service can be stopped correctly.

in AnQiCMS'sstop.shIn the script, the command chain used to stop the core process of AnQiCMS service is:ps -ef | grep '\<anqicms\>' | grep -v grep | awk '{printf $2}'

The purpose of this command chain is to find the main process ID (PID) of AnQiCMS from all running processes. Let's parse it step by step, especiallyawkThe command handles the behavior under multiple PID conditions.

first, ps -efThe command lists detailed information about all running processes in the system.This information usually includes user, PID, parent PID, CPU usage, startup time, and the complete command path.

Then,grep '\<anqicms\>'The command willps -effilter the output of\<and\>are word boundary anchors in regular expressions, they ensuregrepExact match the complete word 'anqicms' rather than a string containing the word (such as 'myanqicms' or 'anqicms_test'), thereby improving the accuracy of the match.

Then,grep -v grepIt is a very common operation. Due to the previousgrep 'anqicms'The command itself is also a running process, which contains the word "grep" in its command line. If this step is not addedgrep -v grepthengrepThe process ID of the command itself is also included in the results, which is obviously not the target process we want to stop.grep -vThe purpose is to perform a reverse match, excluding lines that contain 'grep'.

The core issue lies in the last command in the pipeline:awk '{printf $2}'.awkis a powerful text processing tool, it reads input line by line by default, and splits each line content into fields by spaces or other delimiters. Inps -efthe output format, the second field ($2) is typically the process ID (PID).

Whengrep -v grepThe output contains only one line (i.e., only one AnQiCMS process is running) as an example:user 1234 0.0 0.1 123456 7890 ? Ss Jul01 00:00:01 /path/to/anqicmsat this point,awk '{printf $2}'will extract out.1234And output it as a string. VariableexistsCan correctly obtain1234This PID.

However, ifgrep -v grepThe output contains multiple lines (i.e., there are multiple AnQiCMS processes or related processes running), for example:user 1234 0.0 0.1 123456 7890 ? Ss Jul01 00:00:01 /path/to/anqicms instance1 user 5678 0.0 0.1 123456 7890 ? Ss Jul01 00:00:01 /path/to/anqicms instance2In this case,awkIt will process line by line. For the first line, it will output1234; For the second line, it will output5678. The key isprintfcommand. Andprintcommand is different,printfBy default, no newline character is added after the output content. Therefore,awk '{printf $2}'Will extract all the PID obtainedConcatenate them into a long string without any separators.

This means, if there are two PID1234and5678,awkThe output will be a single string12345678This string will be assigned tostop.shin the scriptexistsVariable.

The script will execute nextkill -9 $exists. Whenexistshas a value of12345678then,kill -9The command will try to kill a process with ID12345678It will not parse it askill -9 1234 5678This long string will not be recognized as multiple independent PIDs. In most cases,12345678Such a PID may not exist, or if it does exist, it is not the actual process of AnQiCMS. Therefore, if there are multiple AnQiCMS processes running, this method will lead tokill -9The command cannot effectively stop all AnQiCMS instances, and may even fail to stop any AnQiCMS instances, thereby preventing the AnQiCMS service from being completely stopped.

Frequently Asked Questions

Q1:stop.shin the scriptawk '{printf $2}'In what circumstances will the command cause AnQiCMS to fail to stop completely?

Related articles

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

How to ensure that the PID of the AnQiCMS process is not reused after system restart or failure?

As an experienced CMS website operation person in a security company, I know the importance of system stability and reliability for a content platform.In daily operations, ensuring the normal operation of core processes and effectively managing their lifecycle is the foundation for ensuring that the website continues to provide services to the outside world.

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

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