Step 1 - Download and Start Supervisor
ubuntu ~ $ sudo apt install supervisor
Check that it's running successfully after installation:
ubuntu ~ $ service supervisor status
is running
Step 2 - Create supervisor Group
ubuntu ~ $ sudo groupadd supervisor
Step 3 - Add User to supervisor Group
ubuntu ~ $ sudo usermod -a -G supervisor ubuntu
Step 4 - Adjust Main Supervisor Config File
Here is the complete /etc/supervisor/supervisord.conf
file:
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0770 ; sockef file mode (default 0700)
chown=root:supervisor
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files cannot
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
In the [unix_http_server]
section we can see the adjusted chmod=0770
directive which will assign group write permissions for for the socket file, along with the chown=root:supervisor
line which will adjust the group of the socket file to our newly created supervisor
group.
Reload the process after adjusting the config file:
sudo service supervisor restart
Restarting supervisor: supervisord.
Step 5 - Add a Program Configuration file in /etc/supervisor/conf.d/
In this example we'll control a Laravel queue; here is the configuration file in /etc/supervisor/conf.d/queue.conf
:
[program:queue]
process_name=%(program_name)s_%(process_num)02d
command=sudo php /var/www/laravel/artisan queue:work --tries=3 --daemon --queue=test -vvv
user=root
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/laravel/storage/logs/test.log
The command
variable tells supervisor which command to run, in our case an artisan command to work on a specific Laravel queue. The stdout_logfile
adds a log file to Laravel's log directory.
Step 6 - Tell supervisor About the New Program Configuration File
Use the reread
supervisor command to make supervisor aware of the new program configuration file we've just created, and then use the update
command to apply the changes:
ubuntu ~ $ supervisorctl reread
queue: changed
ubuntu ~ $ supervisorctl update
ubuntu ~ $ supervisorctl status
queue:queue_00 RUNNING pid 18494, uptime 0:00:02
Helpful Links
- Supervisor documentation: http://supervisord.org/index.html
- Information for steps 2-4: https://github.com/Supervisor/supervisor/issues/173#issuecomment-186128727