Installing Supervisord

We can easily install Supervisord using Python’s pip command. On Ubuntu 16.04, just use the apt-get command:

sudo apt-get install -y supervisor

This installs two tools, supervisor and supervisorctl. Supervisorctl is intended to control the supervisor and add tasks, restart tasks, and so on. Let us use the sample basicServre.go program we created for illustrating Nginx for this too. Install the binary to the $GOPATH/bin directory. Here, suppose my GOPATH is /root/workspace:

go install github.com/narenaryan/basicServer
Always add the bin folder of your current GOPATH to the system path. Whenever you install the project binary, it is available as a normal executable from the overall system environment. You can do it adding this line to the ~/.profile file: export PATH=$PATH:/usr/local/go/bin.

Now, create a configuration file at:

/etc/supervisor/conf.d/goproject.conf

You can add any number of configuration files and supervisord treats them as separate processes to run. Add the following content to the preceding file:

[supervisord]
logfile = /tmp/supervisord.log
[program:myserver]
command=/root/workspace/bin/basicServer
autostart=true
autorestart=true
redirect_stderr=true

By default, we have a file called supervisord.conf at /etc/supervisor/. Look at it for further reference:

  • The [supervisord] section gives the location of the log file for supervisord.
  • [program:myserver] is the task block that traverses to a given directory and executes the command given.

Now, we can ask our supervisorctl to re-read the configuration and restart the task (process). For that, just say:

supervisorctl reread
supervisorctl update

Then, launch our supervisorctl with:

supervisorctl

You will see something like this:

So, our book service is getting monitored by Supervisor. Let us try to kill the process and see what Supervisor does:

kill 6886

Now, as soon as possible, Supervisor starts a new process (different pid) by running the binary:

This is very useful in production scenarios where a service needs to be up in case of any crash or OS restart. One question here, how do we start/stop an application service? Use the start and stop commands from supervisorctl for smooth operations:

supervisorctl> stop myserver
supervisorctl> start myserver

For more details about the Supervisor, visit http://supervisord.org/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.201.217