How to Manage Node.js Processes with PM2
source link: https://travishorn.com/how-to-manage-nodejs-processes-with-pm2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Manage Node.js Processes with PM2
Node.js is a popular runtime environment for building scalable and high-performance applications. However, managing Node.js processes can be challenging, especially when you have multiple processes running on the same machine. PM2 is a process manager for Node.js applications that makes it easy to manage and monitor your Node.js processes. In this article, we’ll walk through the process of installing PM2, creating a configuration file, launching the processes, and setting them to start on system startup.
Setup
Install PM2 globally using npm.
sudo npm install -g pm2
Create the file ~/ecosystem.config.js
. Many of the configuration options in this file will depend on your setup. Here are quite a few examples:
module.exports = {
apps: [
{
// Example for a basic Node.js app
name: "[app name]",
cwd: "/path/to/app",
script: "node",
args: "index.js",
max_restarts: 5,
log_date_format: "YYYY-MM-DDTHH:mmZ",
},
{
// Example for a Node.js app that you normally launch with `npm start`
name: "[app name]",
cwd: "/path/to/app",
script: "npm",
args: "start",
max_restarts: 5,
log_date_format: "YYYY-MM-DDTHH:mmZ",
},
{
// Example for a Next.js app
// This one takes advantage of cluster mode to spawn a new instance
// for every CPU core
name: "[app name]",
cwd: "/path/to/app",
script: "node_modules/next/dist/bin/next",
args: "start",
instances: "max",
exec_mode: "cluster",
max_restarts: 5,
log_date_format: "YYYY-MM-DDTHH:mmZ",
},
{
// Example for a SvelteKit app
name: "[app name]",
cwd: "/path/to/app",
script: "node -r dotenv/config build",
max_restarts: 5,
log_date_format: "YYYY-MM-DDTHH:mmZ",
},
],
};
Make sure you replace [app name]
and /path/to/app
with the appropriate values. I use an absolute path to be extra safe.
Also notice that you can place more than one object in the apps
array if you want to manage more than one Node.js app.
Start the process(es) using PM2.
pm2 start ~/ecosystem.config.js
Open a web browser and navigate to http://[your server's IP address/hostname]:3000. You should see your app. If your app listens on a port other than 3000, just use the appropriate port number instead.
Save the currently running configuration.
pm2 save
View the startup script command.
pm2 startup
That command will output a command that looks something like this:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u myuser --hp /home/myuser
Copy the command.
Paste it into your terminal and press Enter to run it.
PM2 will now automatically start the saved Node.js processes at system startup. Try restarting the machine to test it.
If you make changes to the code, the process for updating the app is...
Push changes to the repository
Log into the server
Pull changes to the cloned (running) app
Install missing dependencies
Rebuild the app
Reload the pm2 processes
You can automate steps 2-6 with continuous integration, which I will go over in a future article.
PM2 is a powerful tool for managing Node.js processes that can help you streamline your development workflow and improve the performance of your applications. By following the steps outlined in this article, you should now have a good understanding of how to install and use PM2 to manage your Node.js processes. Whether you’re working on a small project or a large-scale application, PM2 can help you keep your Node.js processes running smoothly and efficiently.
Cover photo by Omid Armin on Unsplash.
Recommend
-
14
前言 pm2 是 node 进程管理工具,可以利用它来简化很多 node 应用管理的繁琐任务,如性能监控、自动重启、负载均衡等,因为在工作中遇到服务器重启后,需要一个个去重新启动每个服务,这样不仅繁琐、效率低,而且容易遗忘开启一些服务,所以特地对 pm2 进行
-
4
pm2常见命令(1)发布于 今天 15:15 1.1 查看进程/应用的资源消耗情况pm2 monit阅读 23发布于 今天 15:15
-
10
pm2 + ts-node 的那些坑 发表于 2020-11-06 Ecosystem File 按照如下写法 const fs = require("fs");let interpreter;if (process.platform === "win32") { interpreter = "/.bin/ts...
-
11
-
9
新闻动态 COS 每周精选: PM2.5 的数据可视化 王威廉 / 冷静 / 蔡占锐 / 王小宁 编辑:王小宁...
-
1
1,修改package.json{ "name": "ii222", "version": "1.0.0", "private": true, "scripts": { "dev": "nuxt", "build": "nuxt build", "start": "nuxt build && nuxt start", "generate": "nuxt generate" }, //...
-
8
Deploy Angular Project on an ubuntu server with pm2 1. Install NodeJs su...
-
1
<?xml encoding="utf-8" ??>Introduction Node.js is a server-side JavaScript environment that can be used to create high-performance, real-time applications for the web and mobile devices. Managin...
-
3
javascriptA Complete Guide to Node.js Process Management with PM2Ayooluwa Isaiah on Mar 9, 2022
-
4
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK