How to automatically restore the service after the AnQiCMS server restarts using `crontab`?

Calendar 👁️ 66

As an experienced website operations expert, I know that the stable operation of the website is the foundation for the continuous exertion of content value.For systems like AnQiCMS that are committed to providing efficient and reliable content management solutions, ensuring the continuous online service is of great importance.Unexpected server restart, whether it is planned maintenance or an emergency situation, it may lead to the interruption of AnQiCMS services, thereby affecting user access and content presentation.Therefore, mastering how to automatically restore the AnQiCMS service after a server restart is a skill that every operator must have.Today, let's delve into how to use the built-in time task scheduling tool of the Linux systemcrontabTo achieve this goal.

Why is the automatic recovery service so important?

AnQiCMS as an enterprise-level CMS developed based on the Go programming language, with its high-performance architecture, high concurrency features, and SEO-friendly design, all aim to provide users with stable and efficient content services.Whether it is content marketing for small and medium-sized enterprises, self-media publishing of the latest information, or multi-site management, the continuous availability of AnQiCMS is directly related to the website's traffic, user experience, and even brand reputation.Manually intervening to restart the service not only takes time, but also may cause a long service interruption if problems occur during off-hours. BycrontabImplement service auto-recovery to minimize downtime to the greatest extent possible, ensuring that the "lightweight and efficient" concept of AnQiCMS is always throughout the entire life cycle of the website.This has improved the operational efficiency, also indirectly ensuring the website's SEO performance and user retention, in line with the design principles of the AnQiCMS project 'paying attention to high concurrency, security, and scalability.'

The startup and shutdown mechanism of AnQiCMS services

Starting the configurationcrontabPreviously, we first need to understand how AnQiCMS starts and stops. According to the AnQiCMS installation documentstart.md),The project has prepared a dedicated automatic script for the production environment:start.shfor starting,stop.shUsed to stop. These scripts are located in the AnQiCMS installation directory, they encapsulate the complex commands required to start the Go program, and add process checking logic, making service management more convenient.

Start withstart.shFor example, its core logic 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

This script is very intelligent, and it will first check the name ofanqicmsWhether the process is already running. If it is not running(exists -eq 0) it will executenohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &Command to start the service, and redirect the output torunning.logFile, and run the process in the background(nohup ... &)。If the service is already running, the script will not perform any operation. This 'check before start' mechanism is exactly what we are taking advantage ofcrontabto achieve service self-healing.

UtilizecrontabImplement automatic recovery service

crontabIt is a time-based task scheduler tool that comes with the Linux system, which can be used to execute commands or scripts at specified times or frequencies. Combined with AnQiCMS'sstart.shScript, we can easily configure a scheduled task to have the system check the AnQiCMS service status at regular intervals (such as every minute) and automatically start it if the service is not running.

The following are the detailed steps:

1. Confirm the installation path and script executability of AnQiCMS

Before executing anycrontabBefore configuring, please make sure to confirm the installation directory of AnQiCMS, especiallyanqicmsand the executable filestart.shThe accurate location of the script. Assuming your AnQiCMS is installed in/www/wwwroot/anqicmsdirectory, then:

  • Ensurestart.shThe script has execution permissions. If the file does not have execution permissions, you can add it using the following command:
    
    chmod +x /www/wwwroot/anqicms/start.sh
    
  • Run manually oncestart.shTo verify that it works correctly:
    
    cd /www/wwwroot/anqicms
    ./start.sh
    # 稍等片刻,检查AnQiCMS进程是否启动
    ps -ef | grep anqicms
    
    If you seeanqicmsThe process is running, which means the script is valid. If the service is already running, you can stop the service first through./stop.shand then test it../start.sh.

2. Edit Crontab Task List

Enter the following command in your server terminal to edit the current user'scrontabTask List:

crontab -e

If it is the first time you are usingcrontab -eThe system may ask you to select a text editor (for examplevi/nanoetc.). Choose the editor you are familiar with.

3. AddcrontabAutomatically restore task

In the open editor, add a new task entry at the end of the file. It is recommended to add the following content:

# 每分钟检查并启动 AnQiCMS 服务
*/1 * * * * /www/wwwroot/anqicms/start.sh

The meaning of this line of tasks is:

  • */1 * * * *This iscrontabThe time expression, indicating "execute at the 0th second of every minute".
    • The first*/1: minute (every 1 minute)
    • the second*: hour (every hour)
    • the third*: date (every day)
    • The fourth*: month (every month)
    • Fifth*Weekday (any day of the week)
  • /www/wwwroot/anqicms/start.sh: This is the full path of the command to be executed. Please make sure to replace it with the actual installation directory of your AnQiCMS.start.shScript path.

As mentioned earlier,start.shThe script itself checks if AnQiCMS is running. Therefore, evencrontabEvery minute, this script is executed and it does not restart the service repeatedly; it only ensures that the service is restarted when it is interrupted. This is a very robust self-healing strategy.

4. Save and Exit Editor

  • If you are usingviEditor: PressEsckey, then enter:wqand press Enter.
  • If you are usingnanoEditor: PressCtrl + XThen pressYkey to confirm save, finally press Enter.

5. VerifycrontabHas the task been added

After saving and exiting, you can enter the following command again to view yourcrontabtask list, to confirm whether the newly added entry has taken effect:

crontab -l

You should be able to see the task you just added.*/1 * * * * /www/wwwroot/anqicms/start.shThis line.

6. Testing automatic recovery function

In order to verify that the configuration is successful, you can try to manually stop the AnQiCMS service, then wait for one or two minutes to see if the service automatically recovers

Related articles

How to ensure that the AnQiCMS `start.sh` script has the correct execution permissions in `crontab`?

As an experienced website operations expert, I am well aware that the stable operation of AnQiCMS, such an efficient content management system, is inseparable from the careful configuration of every detail.Among them, using `crontab` to guard the `start.sh` script to ensure that the AnQiCMS service remains online is an important means for many operators to ensure the reliability of the website.However, in this process, file execution permission issues often become a "bottleneck" for many users.Today, let's delve into how to ensure your AnQiCMS `start

2025-11-06

After AnQiCMS upgrade, how should the original `crontab` startup task be adjusted and 'save and exit'?

In the daily maintenance of AnQiCMS website operation, system upgrade is the key link to enhance functions, optimize performance, and enhance security.However, after the upgrade, we often need to conduct detailed checks and adjustments on some background tasks, especially those relying on the `crontab` daemon process startup tasks.As an experienced website operation expert, I know the importance of these details for the stable operation of the website.Today, let's delve into how to adjust the original `crontab` startup tasks after the AnQiCMS upgrade, as well as how to correctly 'save and exit' these changes

2025-11-06

How to confirm that the AnQiCMS daemon is running normally after `crontab -e`?

As an experienced website operations expert, I know that a stable running back-end service is crucial for any content management system, especially for enterprise-level solutions like AnQiCMS that pursue efficiency and security.The AnQi CMS is developed based on Go language and has won the favor of many users with its high performance and easy deployment.During the deployment process, ensuring the normal operation of the daemon is the cornerstone of website stability, and how to effectively check after `crontab -e` is the core topic we are discussing today.###

2025-11-06

AnQiCMS task scheduling configuration error, how to safely edit and 'Save and Exit'?

In the daily operation of the website, automated tasks play a crucial role.For a content management system like AnQiCMS that focuses on efficiency and flexibility, scheduled tasks are a powerful tool to enhance operational efficiency.They run silently in the background, performing a series of tasks such as scheduled content release, SEO data update, system maintenance, etc.However, when these plan tasks encounter configuration errors, they often bring considerable inconvenience to the normal operation of the website.Today, let's delve into the configuration error of the AnQiCMS scheduled task

2025-11-06

If AnQiCMS `crontab -e` opens another editor, how to 'save and exit'?

As an experienced website operations expert, I am well aware of the importance of efficient and stable system operation for AnQiCMS and other enterprise-level content management systems.When deploying and maintaining AnQiCMS, we often encounter scenarios where we need to configure scheduled tasks (crontab), such as the `start.sh` script mentioned in the `install.md` that is used to guard the AnQiCMS process.However, when executing the `crontab -e` command, many users may find that the editor opened is not the one they are familiar with

2025-11-06

How to manage the startup of different instances of `crontab` when deploying AnQiCMS across multiple sites?

AnQiCMS with its efficient and flexible architecture provides powerful support for enterprise content management.Especially when dealing with multi-site requirements, AnQiCMS provides various deployment solutions, one of which is to run multiple independent AnQiCMS instances on the same server to manage different websites.How can website operators skillfully use `crontab` scheduled tasks to ensure the stable startup and continuous operation of these different instances when adopting this deployment mode, which has become a key link that needs to be deeply understood by website operators.This article will elaborate on this topic in detail

2025-11-06

What is the role of the `nohup` command in the `start.sh` script for the continuous operation of AnQiCMS?

As an experienced website operations expert, I fully understand the importance of a stable and reliable content management system for a corporate website.AnQiCMS (AnQi CMS) leverages the efficiency and concise architecture of the Go language to provide an excellent content management experience for many enterprises.However, even the most powerful system also needs correct deployment and maintenance to ensure its continuous and efficient operation.Today, let's delve into the seemingly insignificant `nohup` command in the `start.sh` script, and explore its crucial role.

2025-11-06

How to check `crontab` configuration and 'Save and Exit' when AnQiCMS back end is inaccessible?

AnQi CMS is an efficient and customizable enterprise-level content management system, whose stable operation is crucial for website operation.When you suddenly find that the AnQiCMS backend is inaccessible, that anxious feeling is something we operators are all too familiar with.Among many possible reasons, incorrect `crontab` configuration or operation errors during editing are often overlooked yet crucial points.Today, let's delve into how to systematically investigate this issue and master the correct posture for 'save and exit' in `crontab`.

2025-11-06