DevOps / Scripts bash Advanced

Rolling Restart for Node Cluster

Restart a PM2 Node.js cluster one instance at a time to avoid downtime.

bash
#!/bin/bash
APP_NAME=myapp
INSTANCES=$(pm2 list --no-color | grep $APP_NAME | wc -l)
for i in $(seq 0 $((INSTANCES-1))); do
  pm2 restart $APP_NAME --only $i
  sleep 3
  echo Restarted instance $i
done

Prerequisites

  • PM2
  • Node.js cluster

Pro Tips

  • → Check health endpoint between restarts.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
bash
Level
Advanced
Read time
4 min
Published
May 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.