As an experienced website operation expert, I know that every script command carries the responsibility of maintaining system stability.In such an efficient and concise content management system as AnQiCMS, the scheduled task script is an indispensable tool for our daily maintenance and management.stop.shThe internal logic of the script, revealing 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 as an enterprise-level content management system developed based on the Go language, serves 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 pause or restart core services.stop.shThe script was born for this purpose, ensuring that the main process of AnQiCMS can be safely terminated in a simple and efficient manner.This script is usually executed as part of a scheduled task, or when the service needs to be manually stopped.

To better understandstop.shThe working principle, we 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.

Firstly,#!/bin/bashis a standard Shebang, it tells the operating system that this script should be executed by/bin/bashthe interpreter. The following lines are#The text at the beginning is a comment, which explains the purpose, author, and binary file name of the script. This is very helpful for subsequent maintenance and understanding the intent of the script.

The core part of the script starts with variable definition:

  • BINNAME=anqicms: This variable defines the binary executable file name of the AnQiCMS main program, the default isanqicms. This name is the 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 where the script file is located.cdEnter the directory and thenpwdPrint the absolute path of the current working directory.The benefits of doing so are that regardless of where the script is called from, it can accurately know where its 'home' is, thus correctly accessing log files or binary files.

The 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 PID (Process ID) of AnQiCMS process:

  1. ps -efList all running processes on the current system and display their detailed information, including PID, user, CPU usage, etc.
  2. grep '\<anqicms\>':Fromps -efFilter the output to includeanqicmsSuch a line. Here it is used\<and\>It is to ensure an accurate match for the complete word "anqicmsanqicmsThis string, avoid false positives.
  3. grep -v grepbecause of the previous step'sgrepthe command itself will also containanqicmsthis string (for example:grep \<anqicms\>), this step's role is to filter outgrepCommanding its own process, ensuring that we only focus on the AnQiCMS main program's process.
  4. awk '{printf $2}':ps -efThe output format, the second field is usually the process ID (PID).awkThe command is responsible for extracting this field here.printfInstead ofprintIs used to avoid extra newline characters after extracting PID, ensuringexistsThe variable only contains pure PID numbers.

Through this series of exquisite pipeline operations, the script can accurately obtain the PID of the AnQiCMS main program and store it inexiststhe variable.

The script will then record the results 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 to write intocheck.logIn the file, this is very useful for troubleshooting and auditing operation history in the future.
  • echo "PID $BINNAME check: $exists": Also output this information to the console for easy real-time viewing of script execution status.

Finally, the script enters the conditional judgment phase, deciding the next operation based on whether the AnQiCMS process is found.

  • if [ $exists -eq 0 ]; thenIfexistsThe 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 there is no need to stop.
  • elseIfexistsThe value of the variable is not 0, indicating that the PID of AnQiCMS has been successfully obtained and the service is running.
    • echo "$BINNAME is running": Print the 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 immediately kills the target process without allowing the process to perform any cleanup operations