What is the working principle of the `awk '{printf $2}'` command to get the PID in the `stop.sh` script?

Calendar 👁️ 71

As a senior CMS website operator, I fully understand the importance of stable system operation and efficient maintenance.In daily management, understanding the working principles of various automation scripts is the key to ensuring system health.Today, we will delve deeply intostop.shIn the script,awk '{printf $2}'How to accurately locate and obtain the PID (Process ID) of the AnQiCMS process.

In the operation and maintenance practice of AnQiCMS,stop.shThe script is responsible for gracefully closing the application. To achieve this goal, the script needs to first accurately locate the running AnQiCMS main process.The core of this search process is precisely completed by a series of Linux command-line tools working together, whereawk '{printf $2}'The command plays a decisive role in extracting key information - process ID - from the processed text stream.

The entire process of obtaining PID can be regarded as a data processing pipeline that starts withps -efcommand.ps -efUsed to list all running processes on the system and display their detailed information in standard format.This command output includes fields such as the user of the process, PID, parent process PID, CPU utilization, start time, and the complete command path, among many others. These fields are usually separated by spaces.

Immediately thereafter,ps -efThe output of which goes through the pipeline(|Pass togrep '\<anqicms\>'command.grepIt is a powerful text search tool, here its role is to filter out the process lines containing the keyword "anqicms". It is worth noting that,\<and\>Are word boundary markers in regular expressions, they ensuregrepIt will only match the complete word "anqicms", not other processes that contain "anqicms" as a substring, such as those that may exist.anqicms_backupormyanqicmsThus, it avoids misjudgment.

However, merely throughgrep '\<anqicms\>'The filtered results usually containgrepThe command itself. BecausegrepWhen a command is executed, its own command line also contains the word "grep". To exclude this interference, the next link in the pipeline isgrep -v grep. Here,-vThe option indicates a reverse match, it will filter out all lines containing the 'grep' string.By this step, we can get a relatively pure AnQiCMS process list, which only contains actual AnQiCMS application processes.

This is the exact line that describes the AnQiCMS process, but all we need is its process ID. That isawk '{printf $2}'the moment when it takes effect.awkIt is a text processing tool that can read input line by line and split each line into fields according to a specified delimiter, and then perform operations on these fields. It is inps -efIn the standard output of the command, the process ID (PID) is located in the second field. Therefore,$2represents the second field of the current line, which is the PID we need.printfis a formatting output command, similar to the one in C language.printffunction. Here,printf $2the role is to directly print the content of the second field. And withprint $2different,printf $2The default does not add a newline at the end, which makes it very suitable for passing a single value (such as PID) as a command-line argument to subsequent commands.

Finally, throughps -ef | grep '\<anqicms\>' | grep -v grep | awk '{printf $2}'This complete command chain,stop.shThe script can accurately extract the PID of the main process of AnQiCMS. This PID will then be assigned to a variable (such as the variable in the script,exists),then usedkill -9 $existsCommand to forcibly terminate the AnQiCMS application, complete the stop operation.This precise PID acquisition mechanism is the key to ensuring that the script can reliably manage the AnQiCMS process, avoiding the risk of killing other unrelated processes, and ensuring the stability and maintainability of the system.


Frequently Asked Questions (FAQ)

AnQiCMS'stop.shWhy is it that sometimes the AnQiCMS process is not terminated after the script is executed?

This situation may be caused by several reasons. First, the most common reason isgrepThe command could not find the correct process name. Although the script used word boundary matching\<anqicms\>, but if the startup command or executable file name of AnQiCMS changes, it could lead togrepUnable to recognize. Please run manuallyps -ef | grep anqicmsCheck if the process exists and confirm its name. Secondly, insufficient permissions may also be the root cause. If it runsstop.shThe user does not have sufficient privileges to kill the AnQiCMS process (usually requires the same user privileges as the one that started the process or root privileges),kill -9The command will not take effect.

stop.shThe script usedkill -9commands to terminate the AnQiCMS process, what impact will this forced termination method have?

kill -9It is a command that forcibly terminates a process, immediately stopping the target process without giving the process any opportunity to perform cleanup operations, such as saving current data, closing file handles, or releasing resources. In most cases, for applications written in Go language like AnQiCMS, if it does not provide a more elegant shutdown mechanism (such as listening to a specific semaphore for cleanup),kill -9A quick and effective termination method. However, in an ideal situation, it should be preferred to use a milder onekillsignals such askill -15, SIGTERM) because it allows the program to catch signals and perform necessary cleanup work. If AnQiCMS is beingkill -9When terminating, critical data is being processed or write operations are being performed, which may lead to data inconsistency or corruption.

How to verify if AnQiCMS is running after executingstart.shorstop.shDid the script start or stop successfully?

To verify if AnQiCMS has successfully started, you can runstart.shafter runningps -ef | grep '\<anqicms\>' | grep -v grepCommand. If the command returns the line of the AnQiCMS process, it means that the application is running.You can also try to access the AnQiCMS front-end or back-end URL via the browser to confirm that the service is responding normally.To verify whether the operation has been successfully stopped, then executestop.shrun the same command again afterpsif there is no output or only the followinggrepThe line of the command itself indicates that the AnQiCMS process has been terminated.

Related articles

How to manually configure the reliable guard of AnQiCMS process without Baota panel?

As an experienced security CMS operator, I know that it is crucial to ensure the stable operation of the content management system in a complex server environment.For many users who do not use the Baota panel, manually configuring the process guardian for AnQiCMS may seem like a challenge, but as long as the correct method is mastered, it can achieve enterprise-level high reliability.This article will elaborate on how to reliably protect the AnQiCMS process without the Taobao panel.

2025-11-06

Does the PID check logic in the `start.sh` script sufficient to handle various process exception cases?

As an experienced CMS website operation personnel of an enterprise, I am well aware of the importance of stable system operation for content publishing and user experience.`start.sh` script plays a key role in the deployment of AnQiCMS, responsible for checking and ensuring that the AnQiCMS service starts normally.We will delve deeper into the robustness of its PID check logic and its performance in dealing with various process exception situations.### AnQiCMS `start.sh` script's PID check mechanism analysis AnQiCMS `start.sh`

2025-11-06

After AnQiCMS upgrade, if the old process has not been completely stopped, what behavior pattern will the `start.sh` script have?

As an experienced website administrator familiar with AnQiCMS operations, I fully understand that every detail in the system maintenance and upgrade process may affect the stable operation and user experience of the website.The upgrade of the content management system, especially the replacement of the core service process, is a critical link that requires careful operation.Among them, the behavior pattern of the `start.sh` script when the old process is not fully stopped is a key concern for many operations personnel.

2025-11-06

How to modify the `start.sh` script so that it automatically sends an alert email when AnQiCMS fails to start?

As an experienced CMS website operation personnel of an enterprise, I am well aware of the importance of stable system operation to the business.Establishing an automatic alarm mechanism for potential startup failure scenarios of AnQiCMS is crucial for ensuring service continuity.This article will detailedly explain how to modify the `start.sh` script of AnQiCMS so that it can send an alert email in time when starting abnormally, thereby allowing the operation team to obtain information and take countermeasures in the first time.### Understand AnQiCMS's `start.sh` script In AnQiCMS

2025-11-06

How does the automatic startup function of the AnQiCMS process ensure the continuous stable operation of the system if the server restarts unexpectedly?

AnQi CMS, as an enterprise-level content management system built with Go language, has always put system stability and continuous operation capabilities at the core from the very beginning.For any online service, unexpected server restarts or sporadic process termination are inevitable operational challenges.Therefore, AnQiCMS is built with multiple automatic restart mechanisms, aimed at ensuring that the system can quickly and reliably restore services in the face of such sudden situations, to ensure the continuous stable operation of the website.Understanding the importance of automatic pull-up It is crucial to maintain continuous service availability in website operations

2025-11-06

What are the values that can be set for the `BINNAME` variable in the `start.sh` script, besides `anqicms`?

As the website operator of AnQi CMS, we are fully aware that the flexibility of system deployment is crucial for efficient website management.During the deployment of AnQi CMS, the `start.sh` script is a key component for managing the start and stop of core programs.One of the core variables is `BINNAME`, which defines the name of the executable program for AnQi CMS.

2025-11-06

What is the impact on server performance of setting `*/1 * * * *` to check the AnQiCMS process every minute in `crontab`?

As a website operator who is deeply familiar with the operation of Anqi CMS, I know that the stability and response speed of website services are crucial for attracting and retaining users.The content management system is at the core of the website, and its continuous stable operation is the basis for ensuring efficient content publication and smooth user access.Regarding the setting of `*/1 * * * *` in `crontab` to check the AnQiCMS process every minute, as well as the potential impact on server performance, this is a topic worthy of in-depth discussion.### Ongoing intent and implementation Firstly

2025-11-06

How to modify the `start.sh` script to record more detailed debug information when starting AnQiCMS?

As a deep熟悉 AnQiCMS (AnQiCMS) website operator, I know the importance of system stable operation and efficient problem diagnosis for content management.When AnQiCMS encounters exceptions during runtime or requires a deeper understanding of its internal workflow, obtaining detailed debugging information is a crucial step.`start.sh` script is the entry point for AnQiCMS in the Linux environment, which is the key to modifying it to obtain more debugging information.### Understand `start.sh`

2025-11-06