【问题标题】:Restarting a pm2 app selected by user in node.js using pm2 api使用 pm2 api 在 node.js 中重新启动用户选择的 pm2 应用程序
【发布时间】:2021-09-02 17:40:18
【问题描述】:
当列表中有许多其他应用程序时,我们如何使用 pm2 api 从 node.js 重新启动 pm2 应用程序,并且用户可以选择重新启动哪个应用程序?
-currentInfo 是拥有进程信息的事物。
function restart(id) {
for (const elem of currentInfo) {
if (elem.pm_id == id) {
pm2.restart(elem.name, function (err) {
if (err) throw err
console.log("app is restarted");
});
}
}
}
【问题讨论】:
标签:
javascript
node.js
pm2
【解决方案1】:
以下代码将使用特定 id 重新启动 pm2 应用程序(作为函数的参数)我希望这能解决您的问题。
function restartPm2(id) {
return new Promise((resolve, reject) => {
pm2.connect(function (err) {
if (err) reject(err)
pm2.list((err, list) => {
if (err) reject(err)
for (const iterator of list) {
if (iterator.pm_id == id) {
pm2.restart(iterator.name, function (err) {
if (err) reject(err)
resolve("app is restarted");
});
}
}
})
})
})
}
restartPm2(0).then(i => {
console.log(i);
pm2.disconnect()
}).catch(e => {
console.log(e);
pm2.disconnect()
})
其中 0 是应用程序 ID