What is the working principle of the `awk '{printf $2}'` command to get the PID in the `stop.sh` script?
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.