安装
$ 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
可以使用all
或id
代替,all
表示对所有pm2
进程操作,id
表示对某一具有此id
的pm2
进程操作。
管理应用程序列表
$ pm2 [list|ls|status]
开机自启动
设置pm2开机自启动命令:
pm2 startup
//这个命令会在系统 /etc/systemd/system/ 路径下生成一个 pm2-root.service 文件用来开机启动 pm2 服务。
pm2 save
//保存当前 pm2 运行的各个应用保存到 /root/.pm2/dump.pm2 下,开机重启时读取该文件中的内容启动相关应用。