supervisorphp/supervisor
PHP client and wrapper for controlling Supervisor via its XML-RPC API. Manage processes, start/stop/restart programs, read status and logs, and integrate Supervisor control into Laravel or any PHP app with a simple, fluent interface.
Installation
composer require supervisorphp/supervisor
Add the package to your composer.json and run composer install.
First Connection
use Supervisor\Client;
$client = new Client('unix:///var/run/supervisord.sock');
$processes = $client->getAllProcessInfo();
/var/run/supervisord.sock or tcp://127.0.0.1:9001).getAllProcessInfo()—if it returns an array, the connection works.First Use Case: Restart a Process
$client->restartProcess('your_process_name');
Process Management
$client->startProcess('worker:queue');
$client->stopProcess('worker:queue');
$client->restartProcess('worker:queue');
stopProcess() with a timeout to avoid abrupt kills.getAllProcessInfo() to manage multiple processes dynamically.Monitoring and Logging
$status = $client->getProcessInfo('api:server');
if ($status['statename'] !== 'RUNNING') {
// Trigger alert or retry logic
}
tail or logrotate hooks) and correlate with Supervisor events.Event-Driven Actions
// In deploy script (e.g., Laravel Forge/Envoyer)
$client = new Client('unix:///path/to/sock');
$client->restartProcess('app:queue');
Artisan::command() to validate process states.Configuration Validation
$client->reloadConfig(); // Reload after config changes
$errors = $client->getAllProcessInfo(); // Check for 'exitstatus' !== 0
Laravel Artisan Commands:
namespace App\Console\Commands;
use Supervisor\Client;
use Illuminate\Console\Command;
class RestartSupervisorProcess extends Command
{
protected $signature = 'supervisor:restart {process}';
public function handle(Client $client)
{
$client->restartProcess($this->argument('process'));
$this->info("Restarted {$this->argument('process')}");
}
}
app/Console/Kernel.php:
protected $commands = [
Commands\RestartSupervisorProcess::class,
];
Service Providers:
Bind the Client to the container for dependency injection:
$this->app->singleton(Client::class, function () {
return new Client(config('supervisor.socket'));
});
Environment Awareness:
Use .env for socket paths:
SUPERVISOR_SOCKET=unix:///var/run/supervisord.sock
Then inject via config or service provider.
Socket Permissions
Connection refused or Permission denied when connecting to Unix sockets.chmod 666 /var/run/supervisord.sock
unix_http_server config to specify a user/group:
[unix_http_server]
file=/var/run/supervisord.sock
user=www-data
group=www-data
XML-RPC Limitations
Process Name Mismatches
restartProcess() fails if the process name doesn’t match Supervisor’s config exactly (including case sensitivity).getAllProcessInfo() and use the exact string.Race Conditions
stopProcess() + startProcess() with delays.Deprecated Methods
Enable Supervisor Logging:
Add to supervisord.conf:
[supervisord]
logfile=/var/log/supervisor/supervisord.log
logfile_maxbytes=1MB
logfile_backups=5
Tail logs during issues:
tail -f /var/log/supervisor/supervisord.log
Test Locally: Use a local Supervisor instance (e.g., via Docker) to test interactions before deploying.
Error Handling: Wrap calls in try-catch:
try {
$client->restartProcess('process');
} catch (\Supervisor\Exception\SupervisorException $e) {
Log::error("Supervisor error: " . $e->getMessage());
}
Custom Commands
Extend the Client class to add domain-specific methods:
class CustomSupervisorClient extends Client
{
public function restartQueueWorkers()
{
$this->restartProcess('queue:worker-1');
$this->restartProcess('queue:worker-2');
}
}
Event Listeners
Hook into Laravel events (e.g., deployed) to trigger Supervisor actions:
public function handle()
{
$client = app(Client::class);
$client->restartProcess('app:horizon');
}
Monitoring Dashboard Build a Laravel route to display process statuses:
Route::get('/supervisor/status', function () {
$client = new Client(config('supervisor.socket'));
return view('supervisor.status', ['processes' => $client->getAllProcessInfo()]);
});
Configuration Reload Hooks Automate config reloads post-change:
$client->reloadConfig();
sleep(2); // Wait for reload
$client->restartProcess('*'); // Restart all processes
How can I help you explore Laravel packages today?