install 用pm2
安装pm2
PM2 is a process manager. It manages your applications states, so you can start, stop, restart and delete processes.
pm2需要全局安装
cnpm install pm2@latest -g
使用
进入项目根目录
Start an app
pm2 start app.js
Or start any other application easily:
pm2 start bashscript.sh
pm2 start python-app.py --watch
pm2 start binary-file -- --port 1520
As you can see many options are available to manage your application with PM2. You will discover them depending on your use case.
- cli 参数
--name <app_name>
:Specify an app name--watch
:Watch and Restart app when files change--max-memory-restart <200MB>
:Set memory threshold for app reload--log <log_path>
:Specify log file-- arg1 arg2 arg3
:Pass extra arguments to the script--restart-delay <delay in ms>
:Delay between automatic restarts--time
:Prefix logs with time--no-autorestart
:Do not auto restart app--cron <cron_pattern>
:Specify cron for forced restart--no-daemon
:Attach to application log
Managing processes
Managing application state is simple here are the commands
pm2 restart app_name
pm2 reload app_name
pm2 stop app_name
pm2 delete app_name
Instead of app_name
you can pass:
all
to act on all processesid
to act on a specific process id
Check status, logs, metrics
List managed applications
List the status of all application managed by PM2
pm2 [list|ls|status]
Display logs
To display logs in realtime:
pm2 logs
To dig in older logs:
pm2 logs --lines 200
Terminal Based Dashboard
Here is a realtime dashboard that fits directly into your terminal:
pm2 monit
pm2.io: Monitoring & Diagnostic Web Interface
pm2 plus
Updating PM2
pm2 update
CheatSheet
Here are some commands that are worth knowing. Just try them with a sample application or with your current web application on your development machine:
# Fork mode
pm2 start app.js --name my-api # Name process
# Cluster mode
pm2 start app.js -i 0 # Will start maximum processes with LB depending on available CPUs
pm2 start app.js -i max # Same as above, but deprecated.
pm2 scale app +3 # Scales `app` up by 3 workers
pm2 scale app 2 # Scales `app` up or down to 2 workers total
# Listing
pm2 list # Display all processes status
pm2 jlist # Print process list in raw JSON
pm2 prettylist # Print process list in beautified JSON
pm2 describe 0 # Display all informations about a specific process
pm2 monit # Monitor all processes
# Logs
pm2 logs [--raw] # Display all processes logs in streaming
pm2 flush # Empty all log files
pm2 reloadLogs # Reload all logs
# Actions
pm2 stop all # Stop all processes
pm2 restart all # Restart all processes
pm2 reload all # Will 0s downtime reload (for NETWORKED apps)
pm2 stop 0 # Stop specific process id
pm2 restart 0 # Restart specific process id
pm2 delete 0 # Will remove process from pm2 list
pm2 delete all # Will remove all processes from pm2 list
# Misc
pm2 reset <process> # Reset meta data (restarted time...)
pm2 updatePM2 # Update in memory pm2
pm2 ping # Ensure pm2 daemon has been launched
pm2 sendSignal SIGUSR2 my-app # Send system signal to script
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)