CentOS 7 服务器安装pm2

5841 3 年前
PM2是node进程管理工具,可以利用它来简化很多node应用管理的繁琐任务,如性能监控、自动重启、负载均衡等,而且使用非常简单。

安装

$ npm install pm2@latest -g
# or
$ yarn global add pm2

启动node项目

pm2 start /data/website/dsfs/app.js

start也可以轻松启动别的项目

$ pm2 start bashscript.sh
$ pm2 start python-app.py --watch
$ pm2 start binary-file -- --port 1520

可选的参数如下:

# Specify an app name
--name <app_name>

# Watch and Restart app when files change
--watch

# Set memory threshold for app reload
--max-memory-restart <200MB>

# Specify log file
--log <log_path>

# Pass extra arguments to the script
-- arg1 arg2 arg3

# Delay between automatic restarts
--restart-delay <delay in ms>

# Prefix logs with time
--time

# Do not auto restart app
--no-autorestart

# Specify cron for forced restart
--cron <cron_pattern>

# Attach to application log
--no-daemon

进程管理

$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name

app_name可以使用allid代替,all表示对所有pm2进程操作,id表示对某一具有此idpm2进程操作。

管理应用程序列表

$ pm2 [list|ls|status]

开机自启动

设置pm2开机自启动命令:

pm2 startup
//这个命令会在系统 /etc/systemd/system/ 路径下生成一个 pm2-root.service 文件用来开机启动 pm2 服务。
pm2 save
//保存当前 pm2 运行的各个应用保存到 /root/.pm2/dump.pm2 下,开机重启时读取该文件中的内容启动相关应用。
分类栏目
© 2018邮箱:11407215#qq.comGitHub沪ICP备12039518号-6