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.- If
existsThe 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.
- If
- 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:
- the system starts up: Linux system starts after,
cronservices (responsible for executingcrontabtasks) will also start accordingly. 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.- process detection: When
start.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. - The program has started.:
start.shIf AnQiCMS is not running, it will execute.nohup ... &Command, start the main program of AnQiCMS in the background. - Continue running: AnQiCMS program starts, followed by every minute
crontabit 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:
- Terminate AnQiCMS process: Use
ps -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. - Remove or disable
crontabtask: 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 without
systemd(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 of
crontab + 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.