แนะนำ pm2 ใช้รัน server ของ nodejs


คือ node moudle ที่ใช้รัน node.js ได้หลาย process พร้อมกันและยังสามารถ auto restart node ที่ error ให้เราด้วย คำสั่งหลัก

ได้แก่ start, list, stop, delete, restart ติดตั้งด้วยการเปิดหน้าต่าง command ขึ้นมาแล้วใช้คำสั่ง npm install -g pm2 

ในที่นี้จะเป็นการรวบรวมคำสั่งพื้นฐานในการใช้งาน pm2 แบบสรุปย่อๆ ถ้าหากยังไม่มี api มาทดลองใช้ pm2

สามารถไปอ่านบทความสอนสร้าง Rest Api ด้วย Nodejs ได้ที่ สอนสร้าง Rest Api ด้วย Nodejs ตอนที่ 1



pm2 start index.js // รัน nodejs เหมือนใช้ node index.js
pm2 start index.js -n "Hello Wold PM2" // รันแบบมีการตั้งชื่อ node
pm2 start  index.js -i 2 // หมายถึง ทำเป็นโหมด cluster 2 ตัว
หรือ
pm2 start index.js -i max // หมายถึงทำเป็นโหมด cluster มากที่สุดเท่าที่ cpu รองรับ ถ้าไม่ใส่ -i จะเป็นโหมด fork



pm2 list // แสดงโปรแกรมที่รันอยู่ทั้งหมด
pm2 start <app_name> -i 2 // run program ใน mode cluster จำนวน 2 instance
pm2 scale <app_name> 4 // หมายถึงปรับจาก 2 เป็น 4 เฉพาะโหมด cluster




pm2 stop <app_name> // หยุดโปรแกรมตามชื่อที่กำหนด
pm2 stop 0 //หยุดโปรแกรมตาม id ที่กำหนด
pm2 stop all //หยุดโปรแกรมทั้งหมด




pm2 delete <app_name> //ลบโปรแกรมตามชื่อที่กำหนด
pm2 delete 0 //ลบโปรแกรมตาม id ที่กำหนด
pm2 delete all //ลบโปรแกรมทั้งหมด




pm2 restart index //restart โปรแกรมตามชื่อที่กำหนด
pm2 restart 0 //restart โปรแกรมตาม id ที่กำหนด
pm2 restart all //restart โปรแกรมทั้งหมด




pm2 reload <app_name> //reload โปรแกรมตามชื่อที่กำหนด
pm2 reload 0 //reload โปรแกรมตาม id ที่กำหนด
pm2 reload all //reload โปรแกรมทั้งหมด




pm2 info <app_name> // แสดง information ของ program ตามชื่อที่กำหนด
pm2 info 0 // แสดง information ของ program ตาม id ที่กำหนด
pm2 flush // clear log ออกจาก pm2




pm2 startup // หมายถึงเมื่อมีการ start server ให้ program เรา start ด้วย
pm2 save // หมายถึงให้ pm2 เก็บข้อมูลทั้งหมดเพื่อใช้ตอน start
pm2 monit // ดูการใช้ cpu กับ ram แบบ realtime




ความคิดเห็น