如何获取进程的supervisorctl状态?

问题描述 投票:0回答:3

我的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

最好的方法是什么。

php linux
3个回答
9
投票

您可以使用正则表达式提取输出中的前两个字段。或者你可以使用

supervisorctl status | awk '{print $1, $2}'

致谢@Barmar


3
投票

您可以使用

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
键的多维数组。后者是进程状态,请参阅进程状态文档了解可能的值。


0
投票

我对这个问题感兴趣,所以我决定写我的例子:

sudo supervisorctl status | jq -R 'split(" ")|{process:.[0], status:.[1]}' > result.json

也许有帮助

© www.soinside.com 2019 - 2024. All rights reserved.