Node.js 進(jìn)程管理工具 pm2 ,讓你的應(yīng)用程序永不宕機(jī)
在某些情況下,pm2 可能會(huì)出現(xiàn)異常情況導(dǎo)致自動(dòng)退出,例如:
系統(tǒng)出現(xiàn)故障,例如內(nèi)存不足、磁盤空間不足等,導(dǎo)致 pm2 無(wú)法正常運(yùn)行。
pm2 進(jìn)程本身出現(xiàn)問題,例如被意外終止、被殺死等。
Node.js 應(yīng)用程序本身出現(xiàn)問題,例如崩潰、死鎖、內(nèi)存泄漏等,導(dǎo)致 pm2 無(wú)法管理進(jìn)程。
我對(duì)以上的情況寫一個(gè)sh
1>monitorport.sh代碼:
while true; do
? ? # 在這里編寫你的腳本邏輯
? ? # 檢查端口是否已經(jīng)被占用
if ! lsof -i :$1 > /dev/null ; then
? ? # 端口未被占用,啟動(dòng)指定的命令
? ? echo "Port $1 is not in use, starting command: $2"
? ? $2 &
else
? ? # 端口已經(jīng)被占用,輸出提示信息
? ? echo "Port $1 is already in use"
fi
? ? sleep 300? # 等待 5 分鐘
done
2>將以下的命令行放到
nohup /www/nodejsroot/monitorport.sh? ?> /dev/null 2>&1
標(biāo)簽: