What is Supervisor
- It is a Process Control System which allows you to monitor and control a number of processes.
What it can do
- It can be configured to automatically restart processes on a crash.
(operating system signals Supervisor immediately when a process terminates) - It can start processes on its own invocation.
- Processes can be grouped into “process groups” and a set of logically related processes can be stopped and started as a unit.
- It allows to assign priorities to processes and allows the user to start/restart all processes via the supervisorctl client which starts them in the preassigned priority order.
- It also provides password protected web user-interface through which all the processes managed by the supervisor can be stopped/started/restarted when needed.
(Example: you have node server or node web-socket running on the server and it got stopped due to some reason then client having access to web user-interface can start/restart the failed processes without disturbing the developer at midnight)
Platform Requirements
- It will likely work fine on most UNIX systems.
- It will not run at all under any version of Windows. the window has its own way for controlling a process like from services a process can be configured to restart automatically after it failed for around 3 times.
- The supervisor is known to work with Python 2.4 or later but will not work under any version of Python 3.
Installation
- sudo apt-get install supervisor
- After installation, the path for supervisor configuration file will be either “/etc/supervisor/supervisord.conf” or “/etc/supervisord.conf” (Normally the default file is indeed /etc/supervisor.conf, but the Debian distribution patches this to look for /etc/supervisor/supervisor.conf first)
- Refer below link for manual configuration of the file path:
http://supervisord.org/installing.html#creating-a-configuration-file
Supervisor Components
- supervisord
- It is responsible for starting child programs at its own invocation, responding to commands from clients, restarting crashed or exited sub-processes.
- The server process uses a configuration file.
- This is typically located in /etc/supervisord.conf.
- This configuration file is a “Windows-INI” style config file and it’s important to keep this file secure via proper file system permissions because it may contain unencrypted usernames and passwords.
- supervisorctl
- It’s a command-line client which provides a shell-like interface.
- From this, a user can connect to different supervisord processes, get status on the sub-processes controlled by, stop and start sub-processes of, and get lists of running processes of a supervisord.
- Web Server
- Its a web user interface with functionality comparable to supervisorctl may be accessed via a browser if you start supervisord against an internet socket.
- XML-RPC Interface
- The same HTTP server which serves the web UI serves up an XML-RPC interface that can be used to interrogate and control supervisor and the programs it runs.
Set up Programs
- Add a program in supervisord.conf file in below format to run as process and to be controlled by supervisor.
[program:program_one] command=node server.js ;command to be executed, 'node server.js ' is example for running a nodejs server. numprocs=1 ;number of processes copies to start (default 1). directory=<path to command> stdout_logfile=/var/log/supervisor/program_one.log autostart=true ;start at supervisord start (default: true) autorestart=true ;whether/when to restart (default: unexpected) user=root ;setuid to this UNIX account to run the program. stopsignal=KILL ;signal used to kill process (default TERM). startretries=10 ;max # of serial start failures (default 3).
- Below command is to start the program.
supervisorctl start program_one
Set up Group
- Add a Group in supervisord.conf file in below format to run processes as a group and to be controlled as a group by supervisor
[group:group1] programs=program_one, program_two ; Add program names to run as a group priority=999 ; setup priority of the group to run
- Below command is to start the programs in a group.
supervisorctl start <groupname>:* supervisorctl start group1:*
Set up web user-interface
- Add below configuration at the end of supervisord.conf file or edit “[inet_http_server’] section if it already present there.
[inet_http_server] port=0.0.0.0:9001 ;Give any port number which is not already in use. username=<username> ;Give username using which you access web user-interface. password=<password> ;Give password using which you access web user-interface.
- Restart supervisor using below command from command line/terminal.
service supervisor restart
- Access http://localhost:9001/ in a browser to view and control process status through the web interface and control it.
Command list (Use “sudo” before each command to run as root)
- To start, restart and stop supervisor:
sudo service supervisor start sudo service supervisor restart sudo service supervisor stop
To list all programs of supervisor with status:supervisorctl
- To start, restart and stop all programs of supervisor:
supervisorctl start all supervisorctl restart all supervisorctl stop all
- To start, restart and stop specific programs of supervisor:
supervisorctl start program_one supervisorctl restart program_one supervisorctl stop program_one
- To start, restart and stop all programs defined in a group:
supervisorctl start <groupname>:* supervisorctl restart <groupname>:* supervisorctl sttop <groupname>:*
Hope this helps you to let your programs get controlled and monitored automatically.
Thanks for this explanation, I have just discover this blog, nice work man, good concise explanation man, cheers
Its good to know that you liked it. Thanks.