Skip to main content
 首页 » 编程设计

python之如何从 Python 脚本执行 Node js 的 pm2 启动/停止/状态

2025年01月19日12tintown

假设我有一个 Python 脚本,它将使用 subprocess 库来执行一些基本的 pm2 命令(对于 nodejs 代码),如下所示:

import subprocess 
subprocess.call(['pm2', 'status']) 

然后输出以表格格式出现,其中包含在标准输出上启动/运行/出错的所有服务。

如何只获取我想要的相关数据?

例如: 如果我执行以下操作:

subprocess.call(['pm2', 'status', 'someService']) 

我应该只得到 someService

的输出

python 中是否有一个库可以让我这样做?或者我可以在我的 Python 脚本中使用 curl 命令访问 pm2 吗?

请您参考如下方法:

经过一番折腾,我找到了答案。一个简单的 pm2 jlist 检索 JSON 格式的输出。

subprocess.call(['pm2', 'jlist'])