In the daily operation and maintenance of Anqi CMS, ensuring the continuous and stable operation of core services is a crucial link.start.shIn the script, the detection mechanism for the existence of the AnQiCMS process ID (PID) is the key to achieving this goal. Among them,wc -lThe command plays an indispensable role in the detection process.
AnQiCMSstart.shProcess detection mechanism in the script
AnQiCMS'start.shThe script is intended to provide a robust startup and guard mechanism.It is not only responsible for starting the AnQiCMS service, but more importantly, it will periodically check whether the service is still running.If the service stops unexpectedly, this script can automatically restart it to ensure the maximum availability of the website.ps/grepandwcand other tools.
The specific detection command is as follows:
exists=ps -ef | grep 'anqicms' | grep -v grep | wc -l
The purpose of this command is to determine whether a process named "anqicms" exists in the current system and to assign its result (process count) toexistsVariable. Next, we will parse each command in the pipeline and its function to revealwc -lits key position in this chain.
step-by-step parsing and checking the pipeline
first, ps -efThe command is the starting point for process detection. It is used to display detailed information of all running processes on the current system.-eThe option indicates displaying all processes, while-fThe option is displayed in full format, including UID, PID, PPID, C, STIME, TTY, TIME, CMD, and other information, where the CMD column contains the complete command line of the process, which is crucial for accurately identifying specific processes.
Then,grep '\<anqicms\>'The command took overps -efand filter the output.grepIt is a powerful text search tool, here it is used to find lines containing the specific string “anqicms”. It is worth noting that,\<and\>IsgrepThe special regular expression metacharacters that match the beginning and end of a word. This meansgrep '\<anqicms\>'It will exactly match the independent word named "anqicmsThis ensures that we only focus on the AnQiCMS main program, avoiding misjudgment.
Then it isgrep -v grep. When executingps -ef | grep '\<anqicms\>'due togrepIt is also a running process, and the command line may also contain the word “grep”. For example,grep '\<anqicms\>'The command itself will be displayed onps -efThe output contains and its CMD column includesgrep. If not excluded,grep '\<anqicms\>'the output will be more onegrepprocess information of its own.grep -v grepThe function is to reverse filter, excluding all lines containing the "grep" string, so that the final result only contains the target AnQiCMS process and does not include the search forgrepThe process itself. This is a common anti-misjudgment technique in process detection scripts.
wc -l: The key from information flow to quantitative judgment.
At this point, the output of the pipeline has become a pure list that only contains information about the AnQiCMS process (if AnQiCMS is running). Andwc -lThe command plays a decisive role here, transforming the information stream into quantitative data that can be used by the script logic.
wcIs the abbreviation for Word Count (word count), and-lThe option indicates that it counts the number of lines in the input. When it receivesgrep -v grepthe output:
- If the AnQiCMS process is running, then
grep -v grepthe output will include at least one line of information about the AnQiCMS process.wc -lThe number of these lines will be calculated, the result will be a number greater than 0 (usually 1, unless multiple instances are running). - If the AnQiCMS process is not running, then
grep -v grepThe output will be empty, that is, no lines.wc -lThe result of the statistics will be 0.
Therefore,wc -lThe output, namely,existsThe value of the variable, which is directly told.start.shDoes the AnQiCMS script process exist:exists0 indicates that the process does not exist,existsgreater than 0 indicates that the process is running.
wc -lOutput the impact on the script's decision
start.shThe script uses it afterwardsexistsThe value of the variable is used to make decisions:
if [ $exists -eq 0 ]; then
echo "$BINNAME NOT running"
cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
fi
Ifwc -lThe result is 0 (that is$exists -eq 0is true), the script will then judge that the AnQiCMS service is "NOT running", and then executenohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &Command to restart the AnQiCMS service.nohupEnsure the service continues to run after the script or terminal closes.>>Append output to the log file.2>&1Redirect standard error to standard output.&The process will be run in the background.
On the contrary, ifwc -lThe result is greater than 0, the script will judge if the AnQiCMS service is 'is running', and skip the startup command.
Summary
In summary,wc -lThe command in AnQiCMS'sstart.shIn the script, by counting the number of filtered process information lines, it provides a simple and efficient mechanism to determine the running status of the AnQiCMS core service.It converts complex process information streams into a clear digital signal (0 or 1+), which directly drives the script's conditional judgment and automatic restart logic, thereby providing the basic guard capability for the stable operation of AnQiCMS.This design demonstrates the powerful and flexible combination of Unix/Linux command line tools, which is a classic pattern commonly used in automated operations maintenance.
Frequently Asked Questions (FAQ)
1. Why instart.shThe script needs to usenohupTo start the AnQiCMS service?
nohupThe purpose of the command is to allow subsequent commands to continue running in the background even after the user exits the terminal or closes the shell. This means that even if the executionstart.shThe script session is interrupted, the AnQiCMS service will not terminate with it.This is crucial to ensure that the web service continues to run without interruption due to the login/logout of operations personnel.
2. If I change the name of the AnQiCMS executable file,start.shwill the script be affected?
Yes, it will be affected. Instart.shIn the script, there are two places that explicitly mention the name of the executable file:BINNAME=anqicmsandgrep '\<anqicms\>'. If you rename the AnQiCMS executable, for example tomycmsthen you need to modify accordinglyBINNAMEVariable ismycmsand willgrepin the command'\<anqicms\>'changed to'\<mycms\>'. Otherwise, the script will not be able to correctly detect and start your service.
3. Why does AnQiCMS's startup script usually run throughcrontabonce a minute?
tostart.shrun the script throughcrontabSet to run once a minute, which is a key strategy to implement the automatic guardian of AnQiCMS service.This arrangement allows the system to frequently check the running status of AnQiCMS service.If the service stops unexpectedly due to reasons such as memory overflow, program crash, or external attack, the guardian script can detect it within the shortest time (up to one minute) and immediately try to restart the service to minimize the service interruption time and thus improve the availability and stability of the website.