As an experienced website operations expert, I know that every script command carries the responsibility of ensuring the stable operation of the system.In AnQiCMS, such an efficient and simple content management system, the scheduled task script is an indispensable tool for our daily maintenance and management.Today, let's delve into the AnQiCMS task schedulingstop.shThe internal logic of the script, uncovering how it gracefully stops the service and ensures a smooth transition of our website.
In-depth analysis of the AnQiCMS scheduled tasks.stop.shscript
AnQiCMS is an enterprise-level content management system developed based on the Go language, serving a wide range of small and medium-sized enterprises and content operation teams with its high performance and high concurrency features.When performing system maintenance, version upgrades, or troubleshooting, we often need to suspend or restart the core services.stop.shThe script is exactly for this, it ensures that the main process of AnQiCMS can be safely terminated in a concise and efficient manner.This script is usually part of a scheduled task or executed manually when the service needs to be stopped.
In order to understand betterstop.shThe principle of operation, let's first take a look at its typical content:
#!/bin/bash
### stop anqicms
# author fesion
# the bin name is anqicms
BINNAME=anqicms
BINPATH="$( cd "$( dirname "$0" )" && pwd )"
# check the pid if exists
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |awk '{printf $2}'`
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME PID check: $exists" >> $BINPATH/check.log
echo "PID $BINNAME check: $exists"
if [ $exists -eq 0 ]; then
echo "$BINNAME NOT running"
else
echo "$BINNAME is running"
kill -9 $exists
echo "$BINNAME is stop"
fi
Now, let's analyze the internal logic of this script line by line.
first, #!/bin/bashIt is a standard Shebang, which tells the operating system that the script should be executed by/bin/bashthe interpreter. The following lines are used to#The initial text is a comment, which explains the purpose of the script, the author, and the binary file name of the main program, which is very helpful for subsequent maintenance and understanding the intent of the script.
The core part of the script starts with variable definition:
BINNAME=anqicmsThis variable defines the binary executable file name of the AnQiCMS main program, by default,anqicmsThis name is a key identifier when searching and terminating processes.BINPATH="$( cd "$( dirname "$0" )" && pwd )"The purpose of this line of code is to get the absolute path of the current script file.dirname "$0"It will return the directory of the script file.cdEnter the directory thenpwdPrint the absolute path of the current working directory. The benefit of doing this is that regardless of where the script is called from, it can accurately know where its 'home' is, thereby correctly accessing log files or binary files.
Next is the process detection logic, which is alsostop.shthe most critical step:exists=ps -ef | grep 'canqicms' |grep -v grep |awk '{printf $2}'
This line of command is a pipeline operation, it consists of four steps to find the AnQiCMS process ID (PID):
ps -efList all running processes on the current system and display their detailed information, including PID, user, CPU usage, etc.grep '\<anqicms\>': fromps -efFilter the output to includeanqicmsThis line is for example. Here is used\<and\>To ensure an exact match of the complete word 'anqicms', rather than matching 'myanqicms' or 'anqicms_test' and similar containinganqicmsThis string, to avoid misfire.grep -v grepDue to the previous step:grepThe command itself will also includeanqicmsThis string (for example:)grep \<anqicms\>), the purpose of this step is to filter outgrepThe process of the command itself, ensuring that we only focus on the AnQiCMS main program process.awk '{printf $2}':ps -efIn the output format, the second field is usually the process ID (PID).awkThe command is responsible for extracting this field here.printfInsteadprintis used to avoid extra newline characters after extracting PID, to ensureexiststhe variable only contains pure PID numbers.
By performing this series of exquisite pipeline operations, the script can accurately obtain the PID of the main AnQiCMS program and store it inexiststhe variable.
The script will then record the result of this process detection:
echo "$(date +'%Y%m%d %H:%M:%S') $BINNAME PID check: $exists" >> $BINPATH/check.log: Append the timestamp, binary filename, and detected PID information tocheck.logIn the file, this is very useful for troubleshooting and auditing operation history in the future.echo "PID $BINNAME check: $exists": At the same time, output this information to the console for real-time viewing of script execution status.
Finally, the script enters the conditional judgment stage, deciding the next operation based on whether the AnQiCMS process is found:
if [ $exists -eq 0 ]; thenIf:existsThe value of the variable is 0, which means that the AnQiCMS process was not found, i.e., the service is not running.echo "$BINNAME NOT running":The script will print a message indicating that the AnQiCMS service is not running and does not need to be stopped.
elseIf:existsThe value is not 0, indicating that the PID of AnQiCMS has been successfully obtained and the service is running.echo "$BINNAME is running": Prints a message indicating that AnQiCMS is running.kill -9 $exists:This is the critical command to stop the service.killThe command is used to send a signal to the process,-9representSIGKILLSignal, this is a forced termination signal. It will immediately kill the target process without giving it any cleanup operations.