在Debian系統上監控Node.js應用程序的運行狀態有多種方法,以下是一些常用的監控方法:
安裝PM2:
sudo npm install pm2 -g
啟動應用程序:
pm2 start app.js --name "my-app"
查看應用程序狀態:
pm2 status
查看應用程序日志:
pm2 logs my-app
監控實時日志:
pm2 monit
查看資源使用情況:
pm2 top
創建systemd服務文件:
sudo nano /etc/systemd/system/my-app.service
啟動和啟用服務:
sudo systemctl start my-app
sudo systemctl enable my-app
查看服務狀態和日志:
sudo systemctl status my-app
journalctl -u my-app
在你的應用程序中添加以下代碼來創建一個簡單的健康檢查端點:
const http = require('http');
http.createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('OK');
} else {
// Your app's normal routes go here
}
}).listen(3000);
然后,你可以使用curl或其他HTTP客戶端來檢查應用程序的健康狀態:
curl http://localhost:3000/health
prom-client
庫可以采集性能指標,Grafana用來展示數據圖表。以上方法可以幫助你在Debian上有效地監控Node.js應用程序的運行狀態和性能。根據你的具體需求選擇合適的工具進行監控。