This paper introduces the basic usage of PM2. Pm2 commands. Details about the configuration file file.config. js will be explained later.

Important lesson

In the production environment, use global operations such as pm2 kill and pm2 restart all with caution. The author misoperated once, and killed all pM2 instances on the production server. Fortunately, with the help of colleagues, it was restored in a short time, but party A’s company did not realize it.

Generate file.config.js

Enter in the project directory:

pm2 ecosystem
Copy the code

The default configuration file is server.config. js. In actual applications, different configuration files are created for different services, including starting and stopping services.

Start the

Pm2 start(file.config.js) pm2 start app.js (specify a js file)Copy the code

stop

pm2 stop <PID>
Copy the code

Note: Stop just stops, but is not really deleted. Using kill is the real “delete”.

Delete all pM2 managed instances

pm2 kill
Copy the code

Pm2 management is global, no matter which directory you are in, the kill operation will delete all instances! Be careful in a production environment!

Deleting a specified instance

Pm2 del < instance ID/ instance name >Copy the code

After an instance is deleted, run the pm2 list command to check that the instance does not exist. If the configuration file is restarted, an instance with the same name occupies a new instance ID. If the instance ID is retained by Pm2 and is released after the Pm2 kill command is executed, it is considered a new instance. This is different from pM2 Stop.

restart

pm2 startOrGracefulReload ecosystem.config.js
Copy the code
pm2 startOrRestart ecosystem.config.js
Copy the code
Pm2 reload all // reload foobar // reload only foobar instanceCopy the code
pm2 restart ecosystem.config.js
pm2 restart all
Copy the code

The above methods, whether startOrGracefulReload, restart, or reload, do not update the environment variables, that is, even if you modify the environment variables in the file.config.js file, there is no effect. Pm2 provides the –update-env option to do this.

Update the environment variable in the file.config.js file:

pm2 reload ecosystem.config.js --update-env
Copy the code

Note that the restart restarts the process, and if the socket service is used, the port is occupied. — Doubtful: not verified many times.

Only one instance is started

pm2 start ecosystem.config.js  --only app1
pm2 reload ecosystem.config.js  --only app1
Copy the code

Disable PM2 logs

Mp2 logs are stored in < current user directory >/.pm2/logs by default. Unless deleted, mp2 logs grow over time.

pm2 reload ecosystem.config.js  -l logggs
Copy the code

Test 2:

pm2 stop ecosystem.config.js
pm2 start ecosystem.config.js  -l logggs 
Copy the code

Test 3:

pm2 stop ecosystem.config.js
pm2 start ecosystem.config.js  -o "/dev/null" -e "/dev/null"
Copy the code

Note: all are invalid and will be verified further.

Common usage

When starting, use the production environment:

pm2 start --env production
Copy the code

To restart, use the production environment:

pm2 restart --env production
Copy the code

Pm2 plug-in

Log module PM2-Logrotate: Installation:

pm2 install pm2-logrotate
Copy the code

Log:

[PM2][Module] Installing NPM pm2-logrotate module [PM2][Module] Calling [NPM] to install pm2-logrotate ... + [email protected] added 325 packages from 296 polymorphism and Audited 2459 packages in 35.768s found 0 vulnerabilities [PM2][Module] Module downloaded [PM2][WARN] Applications pm2-logrotate not running, starting... [PM2] App [PM2 - logrotate] launched the instances (1) = = PM2 - logrotate = = ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ key │ The value │ ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤ │ max_size 10 m │ │ │ retain 30 │ │ │ compress │ false │ │ dateFormat │ Yyyy-mm-dd_hh-mm-ss │ │ workerInterval │ 30 │ rotateInterval │ 0 0 * * * │ rotateModule │ true │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ [PM2] [the Module] the Module successfully installed and launched [PM2] [the Module] Edit configuration via: `pm2 conf`Copy the code

Use the pm2 list to view running modules.

Delete the pm2 – logrotate:

pm2 uninstall pm2-logrotate
Copy the code

The PM2 runs automatically

If you want pM2 to automatically start after the server restarts, use the root permission to perform the following operations:

Pm2 startup [Platform name] Example: pm2 startup or pM2 startup UbuntuCopy the code

Can. Platform names support the following:

ubuntu, centos, redhat, gentoo, systemd, darwin, amazon.
Copy the code

The actual test found that without the platform name output, PM2 would search for and determine which platform to use.

To cancel automatic startup, run:

Pm2 unstartup or pm2 unstartup UbuntuCopy the code

To save instances managed by PM2, run:

pm2 save
Copy the code

Note: you did not see the command to cancel the save. The test found that the saved instance will be automatically deleted when the pM2 is automatically run after the startup is cancelled.

Some log information is given below. Startup automatic operation:

pm2 startup [sudo] password for latelee: [PM2] Init System found: systemd Platform systemd Template [Unit] Description=PM2 process manager Documentation=https://pm2.keymetrics.io/ After=network.target [Service] Type=forking User=root LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity Environment=PATH=/usr/local/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin Environment=PM2_HOME=/home/latelee/.pm2 PIDFile=/home/latelee/.pm2/pm2.pid ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill [Install] WantedBy=multi-user.target Target path /etc/systemd/system/pm2-root.service Command list [ 'systemctl enable pm2-root' ] [PM2] Writing init configuration in /etc/systemd/system/pm2-root.service [PM2] Making script booting at startup... [PM2] [-] Executing: systemctl enable pm2-root... Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service. [PM2] [v] Command successfully executed. +---------------------------------------+  [PM2] Freeze a process list on reboot via: $ pm2 save [PM2] Remove init script via: $ pm2 unstartup systemdCopy the code

Save example:

# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/latelee/.pm2/dump.pm2
Copy the code

Cancel automatic startup:

pm2 unstartup
[PM2] Init System found: systemd
Removed symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service.

Removed symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service.

[PM2] Init file disabled.
Copy the code

Error and handling:

$ pm2 list
[PM2][ERROR] Permission denied, to give access to current user:
$ sudo chown latelee:latelee /home/latelee/.pm2/rpc.sock /home/latelee/.pm2/pub.sock
Copy the code

Executing sudo chown latelee: latelee/home/latelee /. Pm2 / RPC. The sock/home/latelee /. Pm2 / pub. The sock. The reason is that the system automatically executes as root, and the permissions change.

Cluster pattern

Adjust the number of instances:

Pm2 scale < instance name > < quantity > Pm2 scale app 4Copy the code

Experience with

The resources

www.augustkleimo.com/how-to-run-… Pm2. Keymetrics. IO/docs/usage /…