As an experienced CMS website operation personnel in the security industry, I am well aware of the importance of system stable operation and efficient maintenance.In daily management, understanding the working principles of various automation scripts is crucial for ensuring system health.stop.shIn the script,awk '{printf $2}'How does the command 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 find the running AnQiCMS main process.awk '{printf $2}'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 -efa command.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, PID, parent PID, CPU usage, start time, and the full command path, among many others. These fields are usually separated by spaces.
Immediately following,ps -efthe output of which is passed through the pipeline (|) togrep '\<anqicms\>'a command.grepis a powerful text search tool, here its function is to filter out the process lines containing the keyword 'anqicms'. It is worth noting that, \<and\>They are word boundary markers in regular expressions, ensuringgrepOnly matches the complete 'anqicms' word, rather than other processes that contain 'anqicms' as a substring, for example.anqicms_backupormyanqicmsThus, it avoids false positives.
However, merely throughgrep '\<anqicms\>'the filtered results, will usually includegrepthe command itself. BecausegrepThe command being executed also includes the word "grep" in its own command line. To exclude this interference, the next stage of the pipeline isgrep -v grepHere,-vThe option indicates reverse matching, which filters out all lines containing the string "grep".By this step, we can obtain a relatively pure AnQiCMS process list, which only contains actual AnQiCMS application processes.
Up to this point, we have the accurate line describing the AnQiCMS process, but what we need is merely its process ID. That is exactlyawk '{printf $2}'the moment when it comes into play.awkis a text processing tool that reads input line by line, splits each line into fields based on a specified delimiter, and then performs operations on these fields.ps -efThe standard output of the command contains the process ID (PID) in the second field. Therefore,$2represents the second field of the current line, which is the PID we need.printfis a formatted output command, similar to the one in C language.printffunction. Here,printf $2the role is to directly print the content of the second field. Withprint $2different,printf $2The default does not add a newline at the end of the output, making it suitable for passing a single value (such as PID) as a command-line argument to subsequent commands.
Finally,ps -ef | grep '\<anqicms\>' | grep -v grep | awk '{printf $2}'This is a complete command chain,.stop.shThe script can accurately extract the PID of the AnQiCMS main process. This PID will then be assigned to a variable (such as a variable in the script.)exists),then used forkill -9 $existsCommand to force 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 mistakenly killing other unrelated processes, and ensuring system stability and maintainability.
Common Questions (FAQ)
AnQiCMSstop.shWhy is the AnQiCMS process not terminated sometimes after the script execution?
This situation may be caused by several reasons. First, the most common reason isgrepCommand not found with 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 may lead togrepUnable to recognize. Please run manually.ps -ef | grep anqicms(Note that there are no word boundaries for a more general search) to check if the process exists and confirm its name. Second, insufficient permissions may also be the root of the problem. If it runsstop.shThe user does not have sufficient privileges to kill the AnQiCMS process (usually requires the same user privileges as the one starting 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 forceful termination bring?
kill -9is a command that forcibly terminates a process, immediately stopping the target process without giving it 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 graceful shutdown mechanism (such as by listening to a specific semaphore for cleanup),kill -9is a fast and effective termination method. However, in an ideal situation, it should be given priority to use a more gentle method first.killsignals, such askill -15[en] Because of SIGTERM), it allows the program to catch the signal and perform necessary cleanup work. If AnQiCMS is beingkill -9The system is in the process of handling critical data or performing write operations at the time of termination, which may cause data inconsistency or corruption.
How to verify if AnQiCMS is running after executingstart.shorstop.shthe script successfully starts or stops?
To verify if AnQiCMS has successfully started, you can runstart.shafterps -ef | grep '\<anqicms\>' | grep -v grepCommand.If the command returns the line of the AnQiCMS process, it means the application is running.You can also try accessing the front-end or back-end URL of AnQiCMS via the browser to confirm that the service responds normally.stop.shand then run the samepscommand again, if there is no output or onlygrepThe line of the command itself indicates that the AnQiCMS process has been terminated.