deployment Junior

What is Supervisor?

Short Answer

Supervisor is a process control system that keeps queue workers running 24/7, automatically restarting them if they crash or the server reboots.

Detailed Answer

Supervisor manages long-running processes on Linux: Purpose: - Keeps queue workers alive (they die after max-time or on crash) - Auto-restarts failed processes - Manages multiple worker processes - Logs output for debugging Configuration: - /etc/supervisor/conf.d/laravel-worker.conf - Defines command, user, numprocs, logging - Uses process_name for managing individual workers Operations: - supervisorctl reread: Read new configurations - supervisorctl update: Apply changes - supervisorctl status: Check worker status - supervisorctl restart: Restart workers

Code Example

Example
# Install Supervisor
sudo apt-get install supervisor

# Create config
# /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravelhub/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/laravelhub/storage/logs/worker.log
stopwaitsecs=3600

# Start workers
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start "laravel-worker:*"

# Monitor
sudo supervisorctl status

Interview Tip

Explain why Supervisor is needed: "Without it, queue workers die when the terminal closes. Supervisor keeps them alive and restarts on failure."