我的supervisorctl 正在运行大约50 个进程。现在我想在我的网站上获取这些进程的状态。我的想法是在 php exec(“sudosupervisorctlstatus”) 上使用并将输出设置为数组或类似的东西。我只需要前 2 个冒号。
process1 RUNNING pid 935, uptime 17386 days, 14:52:25
process2 RUNNING pid 936, uptime 17386 days, 14:52:25
process3 RUNNING pid 31907, uptime 0:00:09
最好的方法是什么。
您可以使用
XML-RPC直接与
supervisorctl
进程对话,而不是解析 supervisord
命令的文本输出。主管文档包括支持的 API 调用。
如果将守护进程配置为侦听本地 HTTP 服务器,这是最简单的;假设您将其配置为侦听 localhost 上的默认端口 9001,因此您可以通过 HTTP 连接到 localhost:9001。 PHP 内置了对 XML-RPC 的支持,但您确实需要启用该功能。如果您使用系统的包管理器安装 PHP,请查找要安装的
php-xmlrpc
包。或者,安装纯 PHP XML-RPC 包,例如 polyfill-xmlrpc。
supervisor.getAllProcessInfo()
方法;这将返回一个进程数组,您将需要其中的 name
和 statename
列:
define('SUPERVISOR_RPC_URL', 'http://localhost:9001/RPC2');
function supervisor_states() {
$request = xmlrpc_encode_request("supervisor.getAllProcessInfo", array());
$context = stream_context_create(['http' => [
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
]]);
$file = file_get_contents(SUPERVISOR_RPC_URL, false, $context);
$response = xmlrpc_decode($file);
if (is_array($response) && xmlrpc_is_fault($response)) {
// Consider creating a custom exception class for this.
throw new Exception($response['faultString'], $response['faultCode']);
}
return array_map(
function($proc) {
return ['name'=>$proc['name'], 'status'=>$proc['statusname']];
},
$response,
);
}
这将返回一个带有
name
和 status
键的多维数组。后者是进程状态,请参阅进程状态文档了解可能的值。
我对这个问题感兴趣,所以我决定写我的例子:
sudo supervisorctl status | jq -R 'split(" ")|{process:.[0], status:.[1]}' > result.json
也许有帮助