Run And Manage Your Backend Servers With Supervisor

Manage Your Backend Servers With Supervisor

Content

Introduction

In our daily routine life, we need someone to back us, give us support when required, and uplift us when we fall. That’s the role that Supervisor plays in our backend server.

Currently, the most popular languages for building backend web servers are Javascript and Python, in which the most common frameworks are Node.js and Flask, respectively. So I will configure the supervisor with both of them iteratively. Basically this guide will roam around how you can manage your backend servers with supervisor

In UNIX-like OS, the supervisor allows to monitor and control the number of processes. The supervisor ensures that our web server will continue to run if you push new code, the server reboots/restarts/goes down and back up, etc. If anything goes wrong, it will make sure to create the logs, restart the processes or start the processes at boot time.

The supervisor is a system entirely built on Python and doesn’t need any other compiler to install. It has been very commonly used on many servers for years. It’s compatible and almost works on every OS except Windows. The supervisor is very efficient to manage the processes and can easily be configured, which we will see today in this article.

Components Of Supervisor

There are majorly 4 components of a Supervisor, namely:

  1. supervisord
  2. supervisorctl
  3. Web Server
  4. XML-RPC Interface

Today we will be using supervisord, which is responsible for starting the child programs & logging, and will use supervisorctl, which is the command-line client piece of the supervisor.

Prerequisites

The prerequisites for configuring Supervisor on backend

  1. Deployed & Running backend on some port (for e.g: http://localhost:5000/)
  2. Nginx setup and running 

You can check out these articles if you have any queries related to the above-mentioned points.

Install and Enable Supervisor

To install and enable Supervisor, type the following commands in your machine terminal, iteratively.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install supervisor
sudo systemctl enable supervisor

To run your backend forever, either its python backend or JS backend, let’s configure supervisor. As you have already installed Supervisor, you can change your directory to the configuration directory where you write your program file for supervisor.

cd /etc/supervisor/conf.d/

Create the file with the extension ‘.conf’, inside the configuration directory, in which you can write your program with specific configurations according to your requirements, we are doing the basic one.

Command to create the file in Linux OS: 

sudo touch myserver.conf

Command to write inside the file in Linux OS:

sudo nano myserver.conf

Once you run the ‘nano’ command, the file would be open where you can write your Supervisor configurations. Copy paste the configurations which are mentioned below according to your tech stack of the backend, either its Python or JS, respectively.

Note: It’s a best practice to make the file with the same name as your program name inside the file.

Gunicorn + Flask-Python With Supervisor

Assuming your Flask server is already running with Gunicorn on port (e.g: 8000). Copy the following program in your configuration file (‘/etc/supervisor/conf.d/myserver.conf’).

[program:myserver]
directory=/home/ubuntu/ai-server
command=/home/ubuntu/anaconda3/envs/pose/bin/gunicorn –worker-class gevent –bind 0.0.0.0:8000 run:app –capture-output –log-level=’debug’ –timeout 2400 –workers=5 –worker-connections=1000
autostart=true
autorestart=true
stdout_logfile=/var/log/ai_server/gunicorn.log
stderr_logfile=/var/log/ai_server/gunicorn.err.log

Node.js With Supervisor

Assuming your Node.js server is already running with nvm on some port (e.g:3000), before copying the below program in your supervisor configurations, make sure nodemon is already installed. If it’s not, run the following command in your terminal inside your project directory.

npm install -g nodemon

Also, if you’re not able to run node or npm commands without sudo in your backend project directory, make sure to give it access by running the following command in your terminal.

n=$(which node); \ n=${n%/bin/node}; \ chmod -R 755 $n/bin/*; \ sudo cp -r $n/{bin,lib,share} /usr/local

Reference: StackOverflow

Finally, copy below program in your configuration file (‘/etc/supervisor/conf.d/myserver.conf’).

[supervisord]
nodaemon=true
[program:myserver]
directory=/home/ubuntu/node_backend
command=npm start
autostart=true
autorestart=true
stderr_logfile=/var/log/node_server/myserver.err.log
stdout_logfile=/var/log/node_server/myserver.out.log

Run Supervisor

Hurry! Whether it was your Python Backend on Flask or JS backend on Node frameworks, respectively, you have successfully written the supervisor configurations to run it. Now run the following commands to enable the configurations.

sudo supervisorctl reread
sudo supervisorctl update

To verify that your process is now started successfully, run the following command:

sudo supervisorctl status

It should display you something like the below-attached image:

If it’s not the case, kindly check the error logs, type the following command to do so:

sudo tail -35 /var/log/node_server/myserver.err.log

Now you can listen to your ports, to verify that your backend is up and running.

sudo lsof -i -P -n | grep LISTEN

Display on your terminal would be like this:

Congratulations! You have successfully run your backend forever.

Understanding Of Script

Let’s understand each and every line of program script you have written inside your configuration file.

[program:myserver]
directory=/home/ubuntu/ai-server
command=/home/ubuntu/anaconda3/envs/pose/bin/gunicorn –worker-class gevent –bind 0.0.0.0:8000 run:app –capture-output –log-level=’debug’ –timeout 2400 –workers=5 –worker-connections=1000

At very first, we have given name to our program which would be identity of it, after that we have defined the working directory of our backend project, where our code exists that would run. Now we have written the command which we want our supervisor process to run inside the defined directory. 

autostart=true
autorestart=true

If the machine reboots, we want Supervisor to start the backend automatically, that’s why we set the ‘autostart’ param as true, similarly if anything fails at the backend, we want Supervisor to automatically run the server again so we set ‘autorestart’ param as true.

stdout_logfile=/var/log/ai_server/gunicorn.log
stderr_logfile=/var/log/ai_server/gunicorn.err.log

At the end, we have defined the locations for the log files, as logs are very helpful to find the real cause and debug the issue.

Note: You might need to run the following command to make sure of the existence of your log directory according to the path you mentioned in the script.

sudo mkdir /var/log/ai_server/

Commands For Supervisor

If you have updated anything in your supervisor configuration file, or you have written the config file first time and need to enable supervisor, use the following commands:

sudo supervisorctl reread
sudo supervisorctl update

Command to check the status of the process:

sudo supervisorctl status

To stop your process, run the following command with your program name:

sudo supervisorctl stop myserver

To start your process, run the following command with your program name:

sudo supervisorctl start myserver

Run the following command with your program name to restart your process,

sudo supervisorctl restart myserver

Conclusion

Congrats to you for running your backend forever! I know it wasn’t easy for the first time but you did it well. You have learned about the basics of Supervisor, and how to write the script program to enable and run it.

Hope it helps!

Abdul Hannan

Abdul Hannan

Co-Founder & CEO

Share

Suggested Articles