3.6 KiB
3.6 KiB
Setup pm2 (update Sep 2023)
- configure pm2 like this to start API on system startup
npm install pm2 -g
apt update && apt install sudo curl && curl -sL https://raw.githubusercontent.com/Unitech/pm2/master/packager/setup.deb.sh | sudo -E bash -
- logout and login in again to make the instructions above available
exit
ssh api-data.vbn.de@srv-web-01.vbn-gmbh.local
- install pm2 autocompletion
pm2 completion install
- Update pm2
npm install pm2 -g && pm2 update
- verify installation of pm2
pm2 --version
- initialize pm2 as a startup service
pm2 startup
- to setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/home/api-data.vbn.de/.nvm/versions/node/v18.17.1/bin /home/api-data.vbn.de/.nvm/versions/node/v18.17.1/lib/node_modules/pm2/bin/pm2 startup systemd -u api-data.vbn.de --hp /home/api-data.vbn.de
- start the service as npm start script with PM2
cd ~/git/data-api/
grep start package.json
pm2 start --name api-data.vbn.de npm -- start --watch
- register/save the current list of processes you want to manage using PM2 so that they will re-spawn at system boot (every time it is expected or an unexpected server restart)
pm2 save
- OPTION: restart/logout/login your system, and check if all the serviceis running under PM2
pm2 ls
or
pm2 status
- monitor logs, custom metrics, process information from all processes
pm2 monit
- view details of a single Node process using the process ID or name
pm2 show 0
pm2 show api-data.vbn.de
- list of some common process (single or all) management command
$ sudo pm2 stop all #stop all apps
$ sudo pm2 stop 0 #stop process with ID 0
$ sudo pm2 restart all #restart all apps
$ sudo pm2 reset 0 #reset all counters
$ sudo pm2 delete all #kill and remove all apps
$ sudo pm2 delete 1 #kill and delete app with ID 1
- manage application logs
$ sudo pm2 logs #view logs for all processes
$ sudo pm2 logs 1 #view logs for app 1
$ sudo pm2 logs --json #view logs for all processes in JSON format
$ sudo pm2 flush #flush all logs
- manage the PM2 process
$ sudo pm2 startup #enable PM2 to start at system boot
$ sudo pm2 startup systemd #or explicitly specify systemd as startup system
$ sudo pm2 save #save current process list on reboot
$ sudo pm2 unstartup #disable PM2 from starting at system boot
$ sudo pm2 update #update PM2 package
Setup PM2
- update Debian package repository cache
sudo apt update
- globally install the latest stable version of PM2 via npm
npm i -g pm2
-
create group and user
pm2
following this setup -
generate the startup script
NOTE: This instruction comes back with a reply likeTo setup the Startup Script, copy/paste the following command: sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u <pm2 system service> --hp <pm2 installation home path>
pm2 startup systemd -u pm2
- confirm that the PM2 startup service is up and running under systemd, run the following command
systemctl status pm2-<pm2 system user>.service
Helpful PM2 instructions
- list all node application (process/microservices)
pm2 list
- monitor logs, custom metrics, process information from all processes
pm2 monit
- view details of a single Node.js process using the process ID or name
pm2 show <process ID or name
- update PM2 package
pm2 update