PM2
Installation
Section titled “Installation”# Install PM2 globallynpm install -g pm2
# Update PM2npm install -g pm2@latestpm2 updateStart & Manage Processes
Section titled “Start & Manage Processes”# Start an apppm2 start app.js
# Start with custom namepm2 start app.js --name myapp
# Start with ecosystem configpm2 start ecosystem.config.js
# Start with specific environmentpm2 start ecosystem.config.js --env production
# Auto-restart on file changepm2 start app.js --watch
# Cluster mode (4 processes)pm2 start app.js -i 4
# Memory limit restartpm2 start app.js --max-memory-restart 512MStop & Restart
Section titled “Stop & Restart”# Stop an apppm2 stop myapp
# Restart an apppm2 restart myapp
# Reload with zero-downtime (cluster mode)pm2 reload myapp
# Graceful reloadpm2 gracefulReload myapp
# Remove from PM2pm2 delete myapp
# Stop allpm2 stop all
# Restart allpm2 restart all
# Delete allpm2 delete allProcess List & Status
Section titled “Process List & Status”# List all processespm2 list
# JSON outputpm2 jlist
# Pretty JSONpm2 prettylist
# Detailed infopm2 show myapp
# Restart with updated envpm2 restart myapp --update-env# All logspm2 logs
# Specific app logspm2 logs myapp
# Last 100 linespm2 logs myapp --lines 100
# No streamingpm2 logs myapp --nostream
# Clear logspm2 flush
# JSON formatpm2 logs --json
# Formattedpm2 logs --formatProcess Monitoring
Section titled “Process Monitoring”# Real-time dashboardpm2 monit
# Terminal notificationpm2 dingScaling & Clustering
Section titled “Scaling & Clustering”# Scale to 8 processespm2 scale myapp 8
# Add 3 processespm2 scale myapp +3
# Remove 2 processespm2 scale myapp -2
# Generate startup scriptpm2 startup
# Save current process listpm2 save
# Restore saved processespm2 resurrect
# Remove startup scriptpm2 unstartupEcosystem Config
Section titled “Ecosystem Config”ecosystem.config.js คือไฟล์ config หลักของ PM2 ที่ใช้กำหนดการตั้งค่าต่างๆ ของ app แทนการใช้ command line arguments
พื้นฐาน
Section titled “พื้นฐาน”module.exports = { apps: [{ name: 'myapp', script: 'app.js', }]};name คือชื่อของ app ที่จะแสดงใน PM2, script คือไฟล์ที่ต้องการรัน
โครงสร้างแบบ cluster mode
Section titled “โครงสร้างแบบ cluster mode”module.exports = { apps: [{ name: 'myapp', script: './build/index.js', instances: 4, exec_mode: 'cluster', }]};exec_mode: 'cluster' ใช้สำหรับ app ที่เป็น HTTP server โดย PM2 จะ fork process หลายตัว (4ตัว) ทำ load balancing ให้อัตโนมัติ, เหมาะกับ Next.js, Express, Nuxt
โครงสร้างแบบ fork mode (default)
Section titled “โครงสร้างแบบ fork mode (default)”module.exports = { apps: [{ name: 'api', script: 'app.js', instances: 1, exec_mode: 'fork', }]};exec_mode: 'fork' เป็น default mode จะรัน process เดียว, เหมาะกับ worker, background service, หรือ app ที่ไม่ต้องการ concurrency
Environment Variables
Section titled “Environment Variables”module.exports = { apps: [{ name: 'myapp', script: 'app.js', env: { NODE_ENV: 'development', PORT: 3000, }, env_production: { NODE_ENV: 'production', PORT: 8080, }, }]};env คือตัวแปรสำหรับ environment แบบ general, env_production คือตัวแปรสำหรับ production mode จะถูกใช้เมื่อรัน --env production
Watch & Restart
Section titled “Watch & Restart”module.exports = { apps: [{ name: 'myapp', script: 'app.js', watch: true, watch_delay: 1000, ignore_watch: ['node_modules', 'dist'], max_memory_restart: '512M', restart_delay: 4000, max_restarts: 10, min_uptime: '5s', }]};watch: true เปิดใช้งาน auto-restart เมื่อไฟล์เปลี่ยน, ignore_watch ใช้ exclude folder ที่ไม่ต้องการ, max_memory_restart restartเมื่อใช้ memory เกิน, restart_delay หน่วงเวลาก่อน restart, min_uptime กำหนดเวลาขั้นต่ำที่ app ต้องทำงานก่อนถึงจะนับ restart ว่าสำเร็จ
module.exports = { apps: [{ name: 'myapp', script: 'app.js', error_file: './logs/error.log', out_file: './logs/out.log', log_file: './logs/combined.log', log_date_format: 'YYYY-MM-DD HH:mm:ss', merge_logs: true, }]};error_file กำหนด path ของ error log, out_fileกำหนด path ของ stdout log, log_file รวม log ทั้งหมดไฟล์เดียว, merge_logs รวม log จากทุก process ใน cluster mode
Multiple Apps
Section titled “Multiple Apps”module.exports = { apps: [ { name: 'api', script: 'api.js', env: { NODE_ENV: 'development' }, }, { name: 'worker', script: 'worker.js', env: { NODE_ENV: 'development' }, }, { name: 'scheduler', script: 'scheduler.js', env: { NODE_ENV: 'production' }, env_production: { NODE_ENV: 'production', }, }, ]};สามารถกำหนดหลาย app ในไฟล์เดียวกันได้ โดยแต่ละ app จะมี config แยกกัน ทำให้ start/restart พร้อมกันทั้งหมด
Script Actions (post/start/stop)
Section titled “Script Actions (post/start/stop)”module.exports = { apps: [{ name: 'myapp', script: 'app.js', post_start: './scripts/deploy.sh', post_stop: './scripts/stop.sh', post_restart: './scripts/restart.sh', }]};post_start รัน script หลังจาก start เสร็จ, post_stop รันหลัง stop, post_restart รันหลัง restart (เหมาะสำหรับ deploy hook, cleanup)
Deploy
Section titled “Deploy”module.exports = { apps: [{ name: 'myapp', script: './deploy.sh', deploy: { production: { user: 'root', host: '192.168.1.1', ref: 'origin/main', repo: 'git@github.com:user/repo.git', path: '/var/www/myapp', 'pre-deploy-local': '', 'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production', }, }, }]};deploy ใช้สำหรับ remote deployment ผ่าน SSH โดยกำหนด server info (user, host), git repo, deploy path และคำสั่งที่ต้องรันก่อน/หลัง deploy
คำสั่งที่เกี่ยวข้อง
Section titled “คำสั่งที่เกี่ยวข้อง”# Start with configpm2 start ecosystem.config.js
# Start specific app in configpm2 start ecosystem.config.js --only myapp
# Start with specific environmentpm2 start ecosystem.config.js --env production
# Generate configpm2 init
# Generate config with cluster modepm2 initModule Management
Section titled “Module Management”# Install modulepm2 install pm2-logrotate
# Install from npmpm2 install @pm2/pm2-logrotate
# Uninstall modulepm2 uninstall pm2-logrotate
# Generate local modulepm2 module:generate mymoduleEnvironment Variables
Section titled “Environment Variables”# Set global env varpm2 set MY_VAR "hello"
# Remove env varpm2 unset MY_VARProcess Metadata
Section titled “Process Metadata”# Full detailspm2 describe myapp
# Inspect processpm2 inspect myapp
# Reset restart countpm2 reset myappDocker Tips
Section titled “Docker Tips”# No fork mode (Docker-friendly)pm2 start app.js --no-fork
# Docker CMDCMD [ "pm2-runtime", "start", "ecosystem.config.js" ]