Use pm2 to manage your projects
foreword
The author has always felt that I have docker/k8s in the world, and the operation and deployment of the cluster are always unfavorable. But one premise is ignored. This is a container-based technology, which is used in Linux in most cases. Other containers, such as Windows Container, are troublesome to configure a lot of pitfalls. Sometimes it is necessary to operate in non-Linux scenarios, and this set of implementation is still relatively difficult.
So the author used tauri to develop a desktop application some time ago. By writing the project path, startup script and other information in json, and then importing it as a configuration file, the function of running and deploying the project can be realized by clicking on the application. The ideal is beautiful, but the reality is very skinny. When the function of project restart was developed, because the author knew little about cmd scripts, and because it was annoying to write the interface UI, the project was finally stranded.
However, yesterday I suddenly thought of a rumored npm package—— pm2 , so I went to study it. I feel that the functions are still very powerful and comprehensive. It is very convenient to use in the terminal or Dashboard. In addition, my desktop application It is also possible to carry out more convenient development directly based on pm2.
No more nonsense, let's take a look at what pm2 can do!
Install
You can install pm2 globally, provided that node14+ is installed
npm i pm2 -g
start application
pm2 start :
You can run a node project on the command line, assuming node index.js
you can run a project:
pm2 start -name "node-app" node -- index
You can also write a json, such as an app.json:
{
"apps": {
"name": "node-app",
script: "node",
"args": [
"index"
]
}
}
Then start the project through json:
pm2 start app.json
[Linux only] Assuming your project npm start
runs via npm script:
pm2 start -name "node-app" npm -- start
On windows, you must specify the script as the path of "npm-cli", for example:
pm2 start -n "node-app" "D:\\ProgramSoftware\\nvm\\v16.17.1\\node_modules\\npm\\bin\\npm-cli.js" -- run start
But a cmd black box that cannot be hidden will pop up, if you don’t mind, then do this
or execute the command through cmd:
pm2 start -n "node-app" cmd -- /c npm start
You can also wrap the execution command into a js script, and then let pm2 execute the script:
const {
exec} = require('child_process');
const pros = exec("mvn org.springframework.boot:spring-boot-maven-plugin:run -Dspring-boot.run.jvmArguments='-Dfile.encoding=UTF-8'",{
cwd: process.cwd()});
pros.stderr.on('error', err => console.error(`${
err}`));
pros.stdout.on('data', (data) => {
console.log(`${
data}`);
});
pros.on('error', (err) =>{
console.error(`${
err}`);
})
pros.on('close', (code) => {
console.log(`child process exited with code ${
code}`);
});
Start another language project
pm2 can not only start node projects, but also support projects in other languages! For example java:
terminal style:
pm2 start -n "java-app" java -- -jar example-0.0.1.jar
Json expression:
{
"apps": {
"name": "java-app",
script: "java",
"args": [
"-jar",
"example-0.0.1.jar"
]
}
}
Js formula (can be pm2 init
generated by express):
module.exports = {
apps : [{
name: 'node-app1',
script: './sv1/index.js',
watch: './sv1',
namespace: 'node-demo'
}, {
name: 'node-app2',
script: './sv2/index.js',
watch: './sv2',
namespace: 'node-demo'
}]
};
In addition to these types, other formats such as yml are also supported.
However, one thing that needs to be avoided is that the essence of the script is the address of the executable program. For example, when pm2 is running, assuming java.exe is located under C:\jdk\bin, the actual execution is:
C:\JDK\BIN\JAVA -jar example-0.0.1.jar
What you need to pay attention to is that the whole path is capitalized, but not all languages support capitalization, such as rust:
'CARGO' is not valid proxy name, th valid names are 'cargo', 'rustc'...
For this, you have to specify the full path directly, just like on the npm side:
{
//...
"script": "C:\\Users\\GrapeX\\.cargo\\bin\\cargo.exe"
}
A cmd black box that cannot be hidden will also pop up
, or you have a way to permanently specify CARGO in the system environment, which is equivalent to cargo. I haven't found this method yet.
management application
For an application that already exists in pm2, you can stop and restart it.
pm2 stop <id|name|namespace|all|json|stdin>
: Stop the specified applicationpm2 restart <id|name|namespace|all|json|stdin>
: Restart the specified applicationpm2 reload <id|name|namespace|all|json|stdin>
: instant reload web applicationpm2 delete <id|name|namespace|all|json|stdin>
: delete the specified application
persistent service
With pm2, we don't even need additional tools to download and register services
- First save the pm2 application:
pm2 save
- Register boot self-starting service
Linux:
pm2 startup [--service-name <name>]
Windows: You need to install pm2-windows-startup first
npm i pm2-windows-startup -g
Then install the service:
pm2-startup install
Disable Linux from starting on boot :
pm2 unstartup
Windows:
pm2-startup uninstall
application monitoring
pm2 ls
: View all application status in the terminalpm2 monit
: Open the monitoring panel of the terminalpm2 plus
: In the Dashboard in the browser, I use topm2 plus
report an error , and I can directly access the URL https://app.pm2.io/
view log
pm2 log
: view all application logspm2 log <id|name|namespace|all|json|stdin>
: View the specified log
application cluster
You can divide a series of applications into a namespace as a cluster, and perform operations such as restarting all of them by specifying a namespace.
Taking ecosystem.config.js as an example, this set of applications includes 2 node applications:
module.exports = {
apps : [{
name: 'node-app1',
script: './sv1/index.js',
watch: './sv1',
namespace: 'node-demo'
}, {
name: 'node-app2',
script: './sv2/index.js',
watch: './sv2',
namespace: 'node-demo'
}]
};
load balancing
When an application startup mode is cluster, load balancing will be automatically enabled
static server
Yes, you read that right, pm2 can also act as a static server:
pm2 serve <path> <port>
or via configuration file:
module.exports = {
script: "serve",
env: {
PM2_SERVE_PATH: '.',
PM2_SERVE_PORT: 8080
}
}
application deployment
PM2
Has a simple but powerful deployment system that allows applications to be provisioned and updated in production. This is useful when you want to deploy an application on a bare metal server on one or many servers at a time. For details, see: https://pm2.fenxianglu.cn/docs/general/deployment-system