foreword
The background service program of linux needs to run in the background all the time. If the access is temporarily started through ssh, the service will be shut down directly as soon as the session ends. If you want the service to run in the background and never hang up, I recommend the background service daemon artifact pm2, a powerful background service program suitable for various languages.
Introduction to pm2
For background process management, the commonly used tool is crontab , which can be used in two scenarios: scheduled tasks and resident scripts. Regarding resident scripts, today I will introduce a more useful tool: pm2, a process manager developed based on nodejs, suitable for background resident script management, and has self-built load balancing function for node network applications. pm2 is a process manager for Node applications with load balancing function. pm2 supports multiple development languages, but it has no load balancing ability for processes other than node.
main features
1. Built-in load balancing (using Node cluster cluster module)
2. Running in background
3.0 seconds stop and reload
4. Has startup scripts for Ubuntu and CentOS
5. Stop unstable processes (avoid infinite loops)
6. Console detection
7. Provide HTTP API
8. Remote control and real-time interface API (Nodejs module, allowing to interact with PM2 process manager)
pm2 install
Since pm2 is the process guardian artifact in the nodejs environment, you need to download and install nodejs first. The installation under linux is very simple, just sudo apt-get install nodejs. After downloading and installing, pay attention to replace the mirror source of npm, otherwise the software dependency package will easily fail to download.
Modify to Taobao mirror source
1. command
npm config set registry https://registry.npmmirror.com/
2. Verify command
npm config get registry
If https://registry.npmmirror.com/ is returned , the mirror configuration is successful.
Change to Huawei Cloud mirror source
npm config set registry https://mirrors.huaweicloud.com/repository/npm/
install pm2
npm install pm2 -g
pm2 use
pm2 common commands
Commonly used commands are usually relatively simple. Here are some commonly used management commands for pm2:
-
pm2 start <script_file|config_file> [options] Start the specified application, such as pm2 start index.js --name httpServer;
-
pm2 stop <appName> [options] Stop the specified application, such as pm2 stop httpServer;
-
pm2 reload|restart <appName> [options] Restart the specified application, such as pm2 restart httpServer;
-
pm2 show <appName> [options] Display the details of the specified application, such as pm2 show httpServer;
-
pm2 delete <appName> [options] Delete the specified application, such as pm2 del httpServer. If you modify the application configuration behavior, it is best to delete the application first, and restart it to take effect, such as modifying the script entry file;
-
pm2 kill kills all processes managed by pm2;
-
pm2 logs <appName> View the logs of the specified application, namely standard output and standard error;
-
pm2 monit monitors the cpu and memory usage of each application process;
-
pm2 list, view all process information guarded by pm2.
other resources
npm ERR! Cannot read property 'insert' of undefined error handling_Wu Zhuanjiaya's Blog-CSDN Blog
NPM sets different domestic mirrors_npm domestic mirrors_wangshan_aqi's blog-CSDN blog
Self-use Node.js installation configuration_apt get install nodejs_Moyucheese Blog-CSDN Blog
Detailed explanation of pm2_starky0729's blog - CSDN blog
Use pm2 to manage go application process_pm2 go_Maverick cat a's blog-CSDN blog