Parsing Supervisorctl Output in Web Applications
Exposing the status of a process manager like supervisor to a web interface is a common requirement for monitoring dashboards, but the raw output format presents immediate challenges. When you run sudo supervisorctl status, you get a text stream where each line represents a process group or individual application. The data looks consistent enough at first glance:
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
You need to extract the first two columns (the process name and the state like RUNNING or STOPPED) to determine health. Using PHP's exec() function is a valid approach, but it requires careful handling of output parsing, security considerations, and architectural trade-offs.
Parsing the Raw Output
The most straightforward method is to capture the full stdout from supervisorctl status and process it line-by-line in your application logic. If you are using PHP, you can pipe the command directly into an array split function.
$output = shell_exec('sudo supervisorctl status');
$lines = explode("\n", $output);
$statuses = [];
foreach ($lines as $line) {
if (trim($line) === '') continue; // Skip empty lines
// Split by whitespace, but be careful with process names that might contain spaces
// A regex split is safer than simple explode for the first column
preg_match('/^\s*(.+?)\s+(RUNNING|STOPPED|EXITED)/', $line, $matches);
if (!empty($matches)) {
$statuses[$matches[1]] = [
'state' => $matches[2],
'raw_output' => $line
];
}
}
Using a regular expression to capture the first two logical columns is superior to simple string splitting. Process names can theoretically contain spaces, and relying on explode(' ', $line) will break if a process name is multi-word or if the uptime section contains spaces. The regex pattern anchors to the start of the line (^), captures the process name (greedy but bounded by the next whitespace group), and specifically targets the state keyword which always follows immediately after the name.
Security and Permissions
Running supervisorctl status often requires elevated privileges because the supervisor service runs as a non-root user, while the web server might need to read the .ini files or query the control socket that requires specific permissions. The source material suggests using sudo within the command string:
exec('sudo supervisorctl status')
While this works, embedding sudo directly in a PHP script is a significant security risk if not handled with extreme care. An attacker who compromises your web application could potentially escalate privileges by executing arbitrary shell commands via exec().
The recommended pattern is to configure the system user that runs your web server (e.g., www-data or nginx) to belong to the same group as the supervisor socket, or to use a dedicated service account with read-only permissions on /var/run/supervisor.sock. If you must use sudo, ensure the specific command is allowed via a restricted sudoers entry that does not permit shell access:
# Example /etc/sudoers.d/web-monitor
www-data ALL=(ALL) NOPASSWD:/usr/bin/supervisorctl status
Architectural Alternatives
While parsing supervisorctl output works for simple dashboards, it is an anti-pattern for high-scale or production-critical systems. Polling a process manager via CLI every time a page loads introduces latency and tight coupling between your web app and your infrastructure orchestration layer.
A more robust approach involves having the supervisor configuration itself emit metrics. Modern deployments often use tools like supervisord with the -c flag to specify a config that allows exporting metrics, or integrating with Prometheus exporters that read the state files directly from the filesystem (/var/run/supervisor.sock or /proc/[pid]/status).
However, for small-to-medium applications where a dedicated metric server is overkill, the CLI parsing method remains a pragmatic solution. It requires minimal infrastructure changes and provides immediate visibility without introducing new dependencies. Just remember that this approach relies on the text format of supervisorctl, which could theoretically change in future versions of the tool, though it has remained stable for many years.
If you are managing a large fleet of servers or need real-time health checks, consider integrating with existing orchestration tools like Kubernetes or systemd directly, rather than parsing CLI output from within your web layer.
