As an experienced security CMS website operator, I am well aware of the importance of the stable operation of the content management system to the website business.In routine maintenance, we frequently deal with the startup and shutdown scripts of the system.stop.shThe script usespwdCommand to dynamically determineBINPATHVariable, this reflects the careful consideration of the designer in ensuring the robustness of the system and the ease of user operation.

The Anqi CMS, a system developed based on the Go language and dedicated to providing an efficient and customizable content management solution, requires a clear storage location for its executable files, as well as related logs and configuration files during the deployment and operation process. For the convenience of users, Anqi CMS providesstart.shandstop.shThis Shell script is used to automate the start and stop of services.

First, let's understandBINPATHthe role of these scripts.BINPATHThe variable usually points to the directory where the executable file of the safe CMS 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.log,or look up the running process PID) need to depend onBINPATHto accurately find these positions.

Instart.shin the script, we may seeBINPATHis explicitly set to an absolute path, such as/www/wwwroot/anqicmsThis is usually manually configured by the website administrator at the beginning of the 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 itself. This line of command can be understood in the following steps:

$0In Shell scripts, this means:$0represents the path of the currently executing script file, including the filename. For example, ifstop.shIn/www/wwwroot/anqicms/is in the directory, then$0it may be/www/wwwroot/anqicms/stop.sh.

dirname "$0"is used to$0Extract the directory part, that is, remove the filename. For example, as shown in the above example,dirname "$0"will return/www/wwwroot/anqicms.

cd "$( dirname "$0" )"This step is critical. It changes the current working directory of the Shell session tostop.shThe directory where the script is located. The purpose of doing this is to ensure that subsequent commands are executed relative to the script's own directory.

pwd: IncdAfter the command is executed,pwdThe 'Print Working Directory' command will print the absolute path of the current working directory. Since the previous step has alreadycdReached the directory of the script, thereforepwdThe output at this time isstop.shThe absolute directory of the script.

Finally,$(...)This Shell command substitution syntax, willpwdAssign the output toBINPATHVariables, thus achieving dynamic acquisition of the absolute path of the script's location.

Then, why?stop.shIt is necessary to calculate the path dynamically in such a laborious way, rather than likestart.shWhy use a fixed absolute path directly? This is mainly based on the following important considerations:

Operational flexibility: Website administrators may execute scripts from any directorystop.shfor example/rootIndex, a custom script directory, or directly through/etc/init.d/anqicms stopsuch service management command invocation. Ifstop.shdependent on a hard-coded absolute path