Why does the AnQiCMS `stop.sh` script use the `pwd` command to determine BINPATH?
As an experienced CMS website operation personnel, I am well aware of the importance of the stable operation of the content management system to the business of the website.In routine maintenance, we frequently deal with the startup and shutdown scripts of the system.For the AnQi CMS, itstop.shScripting usespwdCommand to dynamically determineBINPATHVariables, this reflects the designer's careful consideration in ensuring system robustness and user operation convenience.
AnQi CMS is a system developed based on the Go language, committed to providing an efficient and customizable content management solution. During the deployment and operation process, its executable files, as well as related logs and configuration files, all need to have a clear storage location. In order to facilitate user management, AnQi CMS providesstart.shandstop.shThis Shell script automates the start and stop of services.
First, let's understandBINPATHThe role of these scripts.BINPATHThe variable usually points to the directory where the AnQi CMS executable file is located. When the script needs to execute the executable file (such as the startup command) or access the resources in the same directory (such as log filesrunning.log/check.logor need to depend on searching for the PID of a running processBINPATHto accurately locate these positions.
Instart.shIn the script, we might seeBINPATHexplicitly set to an absolute path, for example/www/wwwroot/anqicmsThis is usually manually configured by the website administrator during the initial installation, based on the actual deployment location. It assumes that the executable file of AnQiCMS will always be located at this fixed position.
However, instop.shIn the script,BINPATHThe definition method is more clever and general:BINPATH="$( cd "$( dirname "$0" )" && pwd )"This command does not directly specify a fixed path, but dynamically calculates the absolute path of the script. This line of command can be understood by breaking it down into several steps:
$0In the Shell script,$0represents the path to the current executed script file, including the filename. For example, ifstop.shIn/www/wwwroot/anqicms/In the directory, then$0may be just/www/wwwroot/anqicms/stop.sh.
dirname "$0"This command is used to$0Extract the directory part, that is, remove the filename. For example,dirname "$0"it will return/www/wwwroot/anqicms.
cd "$( dirname "$0" )"This step is crucial. It changes the working directory of the current Shell session tostop.shThe directory of the script. This is done to ensure that all subsequent commands are executed relative to the script's directory.
pwdIncdAfter the command is executed,pwdThe (Print Working Directory) command prints the absolute path of the current working directory. As the previous step has alreadycdReached the directory of the script, thereforepwdWhat is output at this time isstop.shThe absolute directory of the script.
Finally, through$(...)This Shell command substitution syntax, willpwdAssign the output toBINPATHA variable, thus achieving dynamic acquisition of the absolute path of the script's directory.
Then, why?stop.shIt is necessary to calculate the path dynamically with such effort, rather than likestart.shWhy use a fixed absolute path directly? This is mainly based on the following important considerations:
Flexibility of operation: The website administrator may execute from any directorystop.shScript, for example/rootdirectory, a custom script directory, or directly through/etc/init.d/anqicms stopsuch service management command calls. Ifstop.shit depends on a hard-coded absolute path