What is the internal logic of the `stop.sh` script in the AnQiCMS scheduled task?

Calendar 👁️ 52

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):

  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 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.
  3. 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.
  4. 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.

Related articles

How to ensure that AnQiCMS does not automatically start after the server restarts (stop the scheduled task)?

As a senior website operation expert, I know that it is crucial to flexibly control the start and stop of applications when managing website services.AnQiCMS (AnQiCMS) is an efficient content management system based on the Go language, which took into account the ease of deployment and service stability from the beginning. Therefore, it is usually configured to start automatically after the server restarts to ensure the continuity of the website service.This kind of automatic startup is usually achieved through system-level scheduled tasks (such as Cron jobs under Linux or the restart strategy of Docker containers).

2025-11-06

What are the necessary steps and precautions to stop a project before AnQiCMS upgrade?

As a senior website operations expert, I know that every system upgrade is like a precise battle, whether the preparation before it is sufficient or not directly determines the success or failure of the battle.AnQiCMS with its high performance and ease of use, indeed brings us many conveniences, but during its upgrade process, especially during the service suspension phase, we must be meticulous to ensure the safety of data and the smooth transition of business.Before the AnQiCMS project upgrade, we need to suspend the operation of the current service.

2025-11-06

What is the impact on website access after configuring reverse proxy and stopping the AnQiCMS project?

What is the impact on website access after configuring reverse proxy and stopping the AnQiCMS project?Read and understand the mechanism and consequencesWhen deploying AnQiCMS and similar content management systems, we often use a reverse proxy architecture.This deployment method can not only improve website performance but also enhance security, and it is a wise choice for many small and medium-sized enterprises and self-media operators.

2025-11-06

What is the difference between AnQiCMS' 'shutdown prompt' feature and completely stopping the project?

As an experienced website operations expert, I am well aware that it is crucial to manage the website status efficiently at different stages of its lifecycle.AnQiCMS (AnQiCMS) is a content management system known for its efficiency and ease of use, naturally providing operators with tools to deal with various scenarios.Among them, the "shutdown prompt" function and the "complete shutdown project" are two completely different, but very practical ways to manage website status.

2025-11-06

After stopping the AnQiCMS service, will the occupied port be released immediately? How to verify?

As an experienced website operations expert, I know that fine control of server resources is the key to ensuring the stable operation of the website when managing a content management system (CMS).The occupation and release of network ports, especially a seemingly minor aspect that could potentially lead to major problems.Today, let's delve into the discussion of whether the port occupied by the AnQiCMS service will be released immediately after it stops, as well as how we operators should go about verifying it.

2025-11-06

How to analyze the stop state or error logs if AnQiCMS fails during startup?

As an experienced website operations expert, I have been dealing with AnQiCMS (AnQi Content Management System) in my daily work for several years, and I am well aware of its efficiency and simplicity.However, even the most powerful system is prone to encountering problems with "soil adaptation", and the most troublesome is probably the sudden occurrence of *** during the startup process.When AnQiCMS fails to start on time and the website goes offline, how can we calmly analyze its shutdown status or error logs, quickly locate the problem, and solve it?

2025-11-06

What are the preparations needed to restart the AnQiCMS project after a long halt?

Revive a long-dormant AnQiCMS project, which is both a challenge and an opportunity for website operators.A long period of stagnation means that the technical environment may have changed, and the system may have accumulated unresolved problems internally.As an experienced website operation expert, I know that meticulous inspection and comprehensive planning are the foundation for success when restarting the AnQiCMS project.Below, I will detail the necessary preparations from multiple dimensions for you.

2025-11-06

How can you safely stop AnQiCMS from running on MacOS?

As an experienced website operations expert, I am well aware of the importance of a stable and efficient content management system for corporate operations.AnQiCMS (AnQiCMS) leverages the efficiency and rich features of the Go language to provide excellent solutions for many small and medium-sized enterprises.However, whether it is for maintenance, upgrading, or simply needing to temporarily shut down, safely and correctly stopping the operation of AnQiCMS is a fundamental and critical task.

2025-11-06