In AnQiCMS's `stop.sh` script, how does `awk` handle if the `grep` command returns multiple PIDs?
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?