Website operation tool: How to uselsofCommand to check if the AnQiCMS project has stopped successfully and released the port?
As a high-performance content management system developed based on the Go language, AnQiCMS also has precise requirements for the use of server resources while pursuing efficiency.Whether it is to carry out system maintenance, version upgrade, or daily deployment adjustment, ensuring that the AnQiCMS project can stop running cleanly and release the ports it occupies is an indispensable link in website operation.A clean shutdown process can avoid port conflicts, resource waste, and lay a good foundation for the next service start.
If the AnQiCMS project does not stop completely, it may continue to occupy its listening port (usually 8001), causing new service instances to fail to start or cause unnecessary trouble during server resource cleanup.At this point, we need a reliable method to verify whether the project has really been 'closed for rest'.
Get to knowlsofPort check Swiss Army Knife:
In Linux or macOS and other Unix-like systems,lsof(list open files) is a powerful command line tool, as the name implies, it can list all the files currently open on the system.But its functionality is not limited to this, we can also use it to view which processes are using specific network ports. This makeslsofis the ideal tool for us to confirm whether the AnQiCMS port is released.
Verify the practical steps for releasing the port of AnQiCMS
Next, we will explain step by step how to uselsofcommands to check the port status of the AnQiCMS project.
The first step: clarify the port number that the AnQiCMS project is listening to
The AnQiCMS project listens on a specific port to provide services when it starts. According to the default configuration of AnQiCMS, this port is usually8001. But in actual deployment, especially in a multi-site environment, you may have already checked according to the project directory underconfig.jsonThe file or other configuration (such as when deploying on Baota panel or 1Panel, different ports such as 8002, 8003, etc. may have been specified) specified different ports.Before performing the check, please make sure to verify the exact port number used by your current AnQiCMS instance.
Second step: ExecutelsofCheck the command
Once the target port is determined, we can execute the command in the terminal tolsofcheck its occupancy. The command format is very intuitive, just replace the{端口号}confirmed port number in the position:
lsof -i:{端口号}
For example, if your AnQiCMS is running on the default8001port, you will execute:
lsof -i:8001
Step 3: Interpretlsofthe output result of the command
After executing the command, you may see two different results, which clearly tell you the running status of AnQiCMS.
Case one: No output from the terminal (ideal state)
If the terminal does not display any information, or only returns the command prompt without listing any processes, congratulations to you!This means that the specified port is currently not occupied by any process, and the AnQiCMS project has been successfully stopped and the port has been released.You can safely proceed to the next step, such as restarting the service or deploying a new version.
Case two: Terminal output process details (port is still occupied)
IflsofThe command returned output information similar to the following format:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
anqicms 7621 root 3u IPv6 80123 0t0 TCP *:8001 (LISTEN)
This indicates the port8001is still being used by a process namedanqicmsProcess ID (PID) is7621occupied by the process. At this time, the AnQiCMS project has not completely stopped. The output showsCOMMANDthe process name, PIDThe column is the unique identifier of the process,NAMEThe column clearly indicates the port and its status (LISTENindicating that it is listening).
Step 4: Manually terminate any remaining processes (if necessary)
When the port is still occupied, we need to manually terminate the relevant processes. Usually, AnQiCMS under the Linux environment can be executed bystop.shScripts to gracefully stop the service, which allows the program to clean up resources. We strongly recommend that you use the AnQiCMS provided scripts first.stop.shScript to stop the service.
Ifstop.shThe script did not work, or your deployment method did not provide such a script, you can choose to usekill -9Command to force quit the process. Note,kill -9The process will be terminated forcibly, without giving the process a chance to clean up normally, therefore it is usually used as a last resort:
kill -9 7621
Among them7621That is where you arelsofThe process PID found in the output. After executing this command, the process will be terminated immediately. To confirm again, you can run it again.lsof -i:8001. If there is no output at this time, it means that the process has successfully terminated and the port has been released.
Why is a clean shutdown crucial?
Successfully stopping and releasing the port is not just to avoid conflicts, it is also the key to maintaining server health and ensuring the stable operation of AnQiCMS. A clean stop operation can prevent:
- Port conflict:Avoid the newly started service from failing due to the port being occupied.
- Resource leak:Ensure that AnQiCMS releases all occupied memory and other system resources.
- Data corruption:Graceful shutdown allows AnQiCMS to complete the ongoing data operations, reducing the risk of data corruption.
- System stability:Avoid zombie processes or other abnormal states, maintain the cleanliness and stability of the server environment.
MasterlsofCommand, allowing you to operate AnQiCMS daily like an experienced detective, quickly locate and resolve port occupation issues, ensuring smooth website operation without hindrance.
Frequently Asked Questions (FAQ)
Q1: I have run AnQiCMS'sstop.shscript, whylsofis still showing that the port is occupied?A1: How many possibilities are there. First, the script may have failed to terminate the process, for example, due to insufficient permissions or slow response from the process.The process of AnQiCMS may have terminated, and another service quickly occupied the same port.Moreover, in some extreme cases, a 'zombie process' may occur, although the process itself has stopped executing, it still remains in the process table at this timekill -9It may be the only valid cleaning method. You can try checking again after a while or use it directly.kill -9Command cleaning.
Q2:kill -9Is the command safe? Is there a gentler way to stop AnQiCMS?A2:kill -9It is a强制终止命令,it will not give the process any opportunity to clean up or save data, therefore there is a risk of data loss or corruption.In most cases, it is recommended to use a milder approach. For passingstart.sh/stop.shAnQiCMS instance started by the script,stop.shThe script is a gentler, recommended way. It usually sends a SIGTERM signal to the process, allowing the program to clean up and exit gracefully. Only whenstop.shInvalid, or consider using it only when the process does not respond for a long timekill -9.
Q3: If I do not know the specific port where the AnQiCMS project is running, how can I find it?A3: Port configuration of AnQiCMS