As an experienced website operations expert, I am well aware of the importance of an efficient and stable content management system (CMS) for a company's online business.AnQiCMS, with its high-performance and concise architecture based on the Go language, has become an ideal choice for many small and medium-sized enterprises and content operation teams.However, even the most excellent system cannot do without refined operation and maintenance management, which includes how to safely and effectively stop its running process.stop.shHow to configure and execute it?

Graceful shutdown: Why do we need it?stop.sh?

You might think, why not just stop a website service by clicking the stop button directly on the Baota panel? Or is it a simple and rough way?killProcess down?For an application like AnQiCMS, which is developed based on the Go language, its operational mechanism is different from traditional PHP, Java applications.It usually runs as a standalone binary file, requiring more explicit instructions for process management.stop.shThe script is designed to achieve this, it is not just about forcing termination, but also carries the responsibility of locating the correct process and safely closing it, which is crucial for avoiding data corruption and ensuring a smooth startup next time.

stop.shThe core task is to find the currently running AnQiCMS main process and send it a termination signal.This process involves several key steps and clever combinations of Unix/Linux commands.

Unveilingstop.shScript Content

According to the official documentation of AnQiCMS, a typicalstop.shThe script would roughly be like this:

#!/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

Let's break down the essence of this script:

  1. BINNAME=anqicmsThis line defines the name of the AnQiCMS executable file. By default, the executable file of AnQiCMS is usually namedanqicmsIf the file name has been changed, please update it here.
  2. BINPATH="$( cd "$( dirname "$0" )" && pwd )"This line of code is very clever, it automatically gets the absolute path of thestop.shscript and assigns it toBINPATHThis means that no matter where you place the script, as long as it is in the same directory as the executable file of AnQiCMS, it can automatically recognize its running environment.
  3. exists=ps -ef | grep 'anqicms' |grep -v grep |awk '{printf $2}'This is the core command of the script, used to find the process ID (PID) of AnQiCMS.
    • ps -efList the detailed information of all running processes on the current system.
    • grep '\<anqicms\>'Select lines containing the keyword 'anqicms' from the process list.\<and\>The word boundary ensures that only the complete 'anqicms' word is matched, not partial matches like 'anqicms_test'.
    • grep -v grepThis step is to excludegrepProcess the command itself, becausegrepThe command itself will also include the 'grep' keyword to avoid killing by mistake.
    • awk '{printf $2}'Extract the second column of the filtered process information, which is usually the process ID (PID).
  4. if [ $exists -eq 0 ]; then ... else ... fiThis is a conditional judgment.
    • IfexistsThe value (i.e., the found PID) is 0, indicating that the AnQiCMS process is not running, and the script will output 'NOT running'.
    • IfexistsNot 0, it means that the AnQiCMS process is running, the script will output 'is running' and then executekill -9 $existsa command.
  5. kill -9 $existsThis is the command to forcibly terminate the process with the specified PID.-9It is a mandatory termination signal, meaning the process will stop immediately without performing any cleanup. Although it is rough, it is very effective in ensuring the process stops.

Configure in the Baota panel.stop.sh

With an understanding of the script, configuring in the Baota panel becomes effortless.

  1. Log in to the Baota panel and go to 'Scheduled Tasks'Find and click the 'Scheduled Tasks' option in the left menu bar.

  2. Add Scheduled TaskClick the 'Add Scheduled Task' button at the top of the page.

  3. Configure task parameters:

    • Task TypeSelect 'Shell Script'.
    • Task Name: Suggest naming it 'Stop AnQiCMS' or a similar clear name.
    • Execution Schedule: As mentioned in the official documentation.stop.shThe example is to run monthly, which is usually for regular cleaning or in conjunction with scheduled maintenance.But since stopping operations is often temporary and proactive, you can also choose to perform it manually, or set it according to actual maintenance needs (for example, automatically stop before maintenance at a fixed time each month).If you are just for manual stop, you can choose an uncommon period such as 'month', and then trigger it by clicking the execute button.
    • Script Content:Paste the provided abovestop.shscript content into this text box.

    【Key Customization Points】Ensure to check and modify the script in the following before pastingBINNAMEandBINPATHa variable.

    • BINPATHThis should be the root directory of your AnQiCMS program, for example:/www/wwwroot/yourdomain.comYou can find the deployment path of your AnQiCMS in the 'Files' management of the Baota panel.
    • BINNAMEThis is the executable file name of AnQiCMS, usually.anqicmsIf you have renamed it during installation (for example, to distinguish between multiple sites and named)anqicms_site1), please make sure to synchronize the modification.
    • grep '\<anqicms\>'ofanqicmsalso need to be withBINNAMEconsistency.

    For example, if your AnQiCMS is deployed in/www/wwwroot/myanqicmsdirectory, the executable file name isanqicms_mainThen the corresponding part in the script should be modified to:

    #!/bin/bash
    ### stop anqicms
    # author fesion
    # the bin name is anqicms
    BINNAME=anqicms_main # <--- 修改为您的可执行文件名
    BINPATH="/www/wwwroot/myanqicms" # <--- 修改为您的程序根目录
    
    # check the pid if exists
    exists=`ps -ef | grep '\<anqicms_main\>' |grep -v grep |awk '{printf $2}'` # <--- 修改 grep 的匹配名
    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
    
    • Click 'Add Task'Complete the above configuration and click the button to save the task.

Executestop.shscript

When you need to stop the AnQiCMS service, simply find the task you just created in the 'Scheduled Tasks' list of the Baota panel.