How to ensure that the process keeps running after the system restarts when deploying AnQiCMS manually in the command line, by adding `start.sh` to `crontab`?

Calendar 👁️ 77

As an expert in the operation of AnQi CMS, I am well aware of the importance of stable system operation for content management, especially in complex and changing server environments.For users who manually deploy AnQiCMS via the command line, ensuring that the service can automatically recover after system restart is a key link to ensure the continuous online operation of the website.crontabwithstart.shThe collaborative work of scripts to achieve this goal.

The AnQiCMS process keeps running after the system restarts.

In a Linux environment, a system restart will terminate all running user processes.To ensure that AnQiCMS can automatically start and provide services after a restart, we cannot rely solely on manual startup commands.crontabCooperating with a custom one.start.shScripts can solve this problem, which is a common and efficient strategy in server maintenance.

start.shThe working principle of the script

start.shIt is a lightweight Shell script, its core function is to check if the AnQiCMS main program is running, and if it finds that the program is not running, it will start it. The specific content of this script is as follows:

#!/bin/bash
### check and start AnqiCMS
# author fesion
# the bin name is anqicms
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms

# check the pid if exists
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |wc -l`
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"
    cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
fi

Let's break down the key parts of this script:

  • Variable definition:BINNAME=anqicmsDefined the name of the AnQiCMS executable file,BINPATH=/www/wwwroot/anqicmsSpecified the installation path of AnQiCMS. These variables ensure that the script can accurately locate and identify the AnQiCMS program.
  • Process check:ps -ef | grep '\<anqicms\>' | grep -v grep | wc -lIs the core command chain for checking the process.
    • ps -efList all running processes.
    • grep '\<anqicms\>'Filter the lines containing the keyword “anqicms”. Here is the\<and\>Ensure that it is a word boundary, making sure to match the executable file name exactly, not any string containing "anqicms" (such as log file content).
    • grep -v grepFilter outgrepAvoid matching lines generated by the command itself to prevent misjudgment.
    • wc -lCount the number of lines finally matched, which is the number of AnQiCMS processes running.
  • Condition judgment and startup:if [ $exists -eq 0 ]; then ... fiThis section is a logical judgment.
    • IfexistsThe value of the variable is 0 (indicating that the AnQiCMS process is not running), the script will enterifthe block.
    • cd $BINPATHSwitch to the AnQiCMS installation directory, as this ensures the program starts in the correct environment.
    • nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &It is the key to starting AnQiCMS.
      • nohupThe command ensures that the AnQiCMS process continues to run even after the user exits the shell or closes the terminal.
      • >> $BINPATH/running.logRedirect the standard output of the AnQiCMS program torunning.logfile.
      • 2>&1Redirect standard error output to the same position as standard output, i.e.running.log.
      • &Run the AnQiCMS process in the background so that the script can complete immediately.
  • Log recording:echo ... >> $BINPATH/check.logUsed to record the time and process status of each check and startup operation, which is very helpful for troubleshooting later.

crontabscheduling function

crontabIt is a tool used in the Linux system for setting periodic tasks. By addingcrontabwe can let thestart.shscript run automatically at regular intervals.

Ininstall.mdin, the recommendedcrontabEntries are:

*/1 * * * * /www/wwwroot/anqicms.com/start.sh

The meaning of this command is:

  • */1It means execute once a minute.
  • * * * *It represents hours, the day of the month, the month, and the day of the week, which are all used here.*Represents “any” or “all”.

Therefore, the entirecrontabentry means “execute once a minute”/www/wwwroot/anqicms.com/start.shscript.”

Collaborative mechanism that remains running after restart.

When the server encounters an unexpected restart or the administrator initiates a system restart, the AnQiCMS process will be terminated. At this point,crontabwithstart.shthe collaborative mechanism begins to take effect:

  1. the system starts up: Linux system starts after,cronservices (responsible for executingcrontabtasks) will also start accordingly.
  2. start.shscheduled:cronThe service will execute within the first minute cycle of its operation (for example, within the first minute after system startup)crontabconfigured/www/wwwroot/anqicms.com/start.shscript.
  3. process detection: Whenstart.shIt will pass when executedps -ef | grepThe command checks if the AnQiCMS main program is running. Since the system has just been restarted, the AnQiCMS process has not started yet, soexistsThe value of the variable will be 0.
  4. The program has started.:start.shIf AnQiCMS is not running, it will execute.nohup ... &Command, start the main program of AnQiCMS in the background.
  5. Continue running: AnQiCMS program starts, followed by every minutecrontabit will call againstart.sh. At this point,start.shit will detect that the AnQiCMS process already exists(existsThe variable is not 0), it will not execute the startup command, thereby avoiding repeated startups and the generation of multiple AnQiCMS instances.

In this way, even if the AnQiCMS process stops for any reason (such as system restart, program crash, etc.), it can be restarted in a very short time (up to one minute)crontabRe-detected and automatically launched to ensure the continuous availability of AnQiCMS services to the maximum extent possible.


Frequently Asked Questions (FAQ)

1. Ifstart.shThe script path or the name of the AnQiCMS executable file has changed, how do I update the configuration to ensure it still works after restart?

You need to manually edit yourcrontabEntries andstart.shScript. First, usecrontab -eCommand editcrontabFile, update the full path of the script to the new path. Second, openstart.shScript file, modifyBINNAMEandBINPATHVariable, make it point to the new name and path of the AnQiCMS executable file. Ensuregrep '\<anqicms\>'the program name in it alsoBINNAMEremain consistent.

2. How do I manually stop the AnQiCMS service if it is started bycrontabregular checks and pulls?

Simply terminating the AnQiCMS process is not enough becausecrontabIt will restart it again in one minute. To completely stop the service, you need to perform the following steps:

  1. Terminate AnQiCMS process: Useps -ef | grep '\<anqicms\>' | grep -v grep | awk '{print $2}'Find the PID of the AnQiCMS process and then usekill -9 <PID>the command to terminate it.
  2. Remove or disablecrontabtask: Usecrontab -ethe command to edit yourcrontabFile, delete or comment out (add at the beginning of the line)#) AnQiCMS related startup tasks. Save and exit. After completing these two steps, AnQiCMS will no longer run, andcrontabIt will not try to start it again.

3. Why not usesystemd(orsysvinit) and other system service management tools to manage the AnQiCMS process, but choosecrontab?

crontabThe method has an advantage in some specific scenarios, especially in manual command line deployment and pursuit of lightweight.

  • Deployment simplicity: For users without system service configuration experience, or withoutsystemd(such as some old or minimalist Linux distributions),crontabProvided a simple method to achieve self-startup and process guarding without additional system service file configuration.
  • Cross-platform compatibility:crontabandstart.shThe script is a standard Unix/Linux tool, supported by almost all Linux systems, and has good compatibility.
  • Process supervision: This kind ofcrontab + start.shThe combination can not only start the program after the system restart, but also play a simple process protection role, if the AnQiCMS process crashes unexpectedly, it will also be automatically restarted within one minute.

However, in modern Linux distributions,systemdIt is generally considered to be a more robust and feature-rich service management tool, providing finer control over startup sequence, log management, and resource limits, and is recommended for production environments. The document usescrontabThe solution mainly focuses on the convenience of manual deployment.

Related articles

What impact will there be on the PID management if `cd $BINPATH` fails when AnQiCMS starts?

As an experienced CMS website operation person in an enterprise, I am well aware of the importance of system stability for content release and user experience.AnQi CMS as an efficient Go language content management system, its startup mechanism, especially PID management, is a key link to ensure the service is online continuously.When the `cd $BINPATH` command in the startup script fails, it will cause a cascade effect on the subsequent process ID (PID) management, which in turn will affect the normal operation of the entire CMS.The startup script of AnQi CMS, such as the `start.sh` mentioned in the document

2025-11-06

How to write a AnQiCMS monitoring script that not only checks PID but also detects if the port is listening normally?

As a senior AnQiCMS website operation personnel, I am well aware of the importance of maintaining system stability for content distribution and user experience.Our AnQiCMS, developed based on the Go language, is renowned for its high performance and stability, but any system may encounter unexpected problems in complex production environments.Therefore, a reliable monitoring mechanism is indispensable.

2025-11-06

What is the meaning of `PID check: 0` in the `check.log` after the AnQiCMS process exits abnormally?

As an experienced CMS website operation personnel of an enterprise, I know the importance of stable system operation for the content platform.In daily work, we often need to pay attention to various system logs in order to discover and solve potential problems in a timely manner.Among them, the `PID check: 0` record in the `check.log` file is a very critical signal when we are troubleshooting the abnormal process of AnQiCMS.

2025-11-06

If the AnQiCMS process cannot be terminated with `kill -9`, could it be related to the PID?

As a senior CMS website operation personnel of a well-known security company, I know that process control is a key link in maintaining the stable operation of the website in daily management.When encountering a situation where it is necessary to forcibly terminate the AnQiCMS process but the `kill -9` command seems to be ineffective, this is indeed a headache.Below, I will elaborate on the possible reasons from my experience and discuss the role of process ID (PID) in it.

2025-11-06

How does the Baota panel's Go project deployment method differ from the PID management mechanism of the AnQiCMS built-in script?

AnQi CMS is a corporate-level content management system developed based on the Go language, which is favored by many small and medium-sized enterprises and content operation teams for its easy deployment, security and efficiency.In the Linux server environment, Baota panel, as a widely popular server operation and maintenance tool, provides great convenience for the deployment of AnQiCMS.This article will discuss in detail how to deploy the AnQiCMS Go project on the Baota panel, and analyze the similarities and differences in its PID management mechanism and the scripts provided by AnQiCMS.### Panel deployment of AnQiCMS Go

2025-11-06

In the AnQiCMS multi-site deployment tutorial, how is the PID of different sites distinguished and managed?

As an experienced security CMS website operator, I fully understand the great value of multi-site deployment in content management and efficiency improvement.Regarding your question about how different site PIDs are distinguished and managed in the AnQiCMS multi-site deployment tutorial?This question, I will elaborate on the mechanism of Anqi CMS design concept and practical operation for you.In the multi-site deployment scenario of AnQi CMS, distinguishing and managing the PID (Process ID, process ID) of different sites mainly depends on the deployment method you adopt

2025-11-06

How to assist in troubleshooting the issue of AnQiCMS process PID not existing but the website is inaccessible by checking the system logs?

As an experienced CMS website operation person, I know the anxiety of the website being inaccessible but the process PID is not traceable.In this case, the system log is the key 'eye' for us to understand the root cause.The AnqiCMS is developed based on the Go language, its efficient and stable characteristics make it rarely encounter problems during normal operation, but when it does, it often means that the startup environment or external dependencies have issues.### Overview of AnQiCMS process startup mechanism AnQiCMS usually manages its processes through a startup script (such as `start.sh`)

2025-11-06

What is the role of `>>> $BINPATH/running.log 2>&1` in the AnQiCMS startup script, is it related to the process PID?

As an expert in AnQiCMS (AnQiCMS) operation, I know the importance of every system detail for the stable operation of the website.Every step in the startup script is crucial, as it directly affects whether the service can start normally and whether we can quickly locate and resolve issues when problems arise.Today, let's delve into the actual function of the command `>> $BINPATH/running.log 2>&1` in the AnQiCMS startup script, as well as its relationship with the process PID.### AnQiCMS

2025-11-06