7

I have supervisorctl running with about 50 processes. Now I want to get the status of these processes on my website. My idea is to use on php exec(“sudo supervisorctl status”) and set the output to a array or something like that. I only need the first 2 colons.

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

What is the best way to do this.

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
Lewis
  • 145
  • 2
  • 2
  • 8
  • You mean the two colons in `14:52:25`? – Barmar Aug 08 '17 at 17:43
  • 6
    You can use a regular expression to extract the first two fields in the outpu. Or you could use `supervisorctl status | awk '{print $1, $2}'` – Barmar Aug 08 '17 at 17:45
  • 1
    Thanks, supervisorctl status | awk '{print $1, $2}' was the solution. I have now exec("sudo supervisorctl status | awk '{print $1, $2}'", $output); And on $output I have the array that I want. – Lewis Aug 09 '17 at 12:07
  • 1
    `sudo supervisorctl status | awk '{print $1, $2}'` showed me that 8 Laravel workers are running. https://laravel.com/docs/7.x/queues#supervisor-configuration – Ryan Sep 12 '20 at 19:06
  • What have you tried so far? Where are you stuck? If this is really a PHP problem, share the code that you are using – Nico Haase Feb 10 '23 at 07:32

2 Answers2

6

You can use a regular expression to extract the first two fields in the outpu. Or you could use supervisorctl status | awk '{print $1, $2}'

Credits to @Barmar

Carlos.V
  • 409
  • 6
  • 13
1

Instead of parsing the text output from the supervisorctl command, you could talk directly to the supervisord process using XML-RPC. The supervisor documentation includes the supported API calls.

It's easiest if you configure the daemon to listen on a local HTTP server; let's assume you configured it to listen on the default port 9001 on localhost, so you can connect over HTTP to localhost:9001. PHP has built-in support for XML-RPC but you do need to enable the feature. If you installed PHP using your system's package manager, look for a php-xmlrpc package to install. Alternatively, install a pure PHP XML-RPC package like polyfill-xmlrpc.

To get the status of all managed processes, use the supervisor.getAllProcessInfo() method; this returns an array of processes, and you'll need the name and statename columns from those:

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,
    );
}

This returns an multidimensional array with name and status keys. The latter is the process state, see the Process States documentation for possible values.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343