working with supervisor on ubuntu


working with supervisor on ubuntu

In most of the ubuntu virtual private servers it is often case that we will have to run several small programs continuously. The most traditional way to do it is to write a init script in for the program that we want to run continuously. But it is a time consuming process to manage it.

Supervisor is a process manager which manages the long running programs by providing an interface which is used to monitor and manage the state of the long running programs.

Supervisor installation on Ubuntu

To install the supervisor on ubuntu we need run the below command in terminal

sudo apt-get install supervisor

After completing the successful installation of supervisor it should automatically starts working.

  • To know the supervisor status run the below command(status can be active or inactive)
    sudo service supervisor status
  • To start the inactive supervisor then run the below command
    sudo service supervisor start
  • To stop the running supervisor then run the below command
    sudo service supervisor stop
  •  To restart the supervisor and all the programs that it manages then run the below command
    sudo service supervisor restart
  • We can find the supervisor logs in /var/log/supervisor/supervisord.log
  • Supervisor comes with the command supervisorctl which allows us to manage the added programs to the supervisor. 

Add a program to Supervisor

  • The program configuration files for Supervisor programs are found in the/etc/supervisor/conf.ddirectory.
  • Let's see the configuration syntax of process to add
    [program:<program_name>]
    environment=KEY="val",KEY2="val2"
    command=<command to run the program ex: /usr/bin/local/myscript.sh>
    autostart=true
    autorestart=true
    stderr_logfile=<error log file location ex: /var/log/myscript_err.log>
    stdout_logfile=<error log file location ex: /var/log/myscript_out.log>
    
  • From above syntax we can understand that supervisor manages the program with name and if  "autostart=true" then it will run the program autmatically when supervisor updates the configurations.
  • If "autorestart=true" then the program/process will automatically restarts if program/process exit's due to some reason.
  • We can see program/process standard output in the log file that we specified in "stdout_logfile".
  • We can see programs/process standard error output in the log file that we specified in "stderr_logfile".
  • We can define the environment variable using "environment" in supervisor these environment variables can be used inside the program.
  • Once the configuration file is created in the directory /etc/supervisor/conf.dthen we need to tell the supervisor to check for the new programs. we can do it with below command. It is also used to check for updates in the existed configirations and also it will check the syntax of the configuration files. 
    sudo supervisorctl reread
  • After, we need to update the configurations. We can do it with the below command.
    sudo supervisorctl update

Manage the supervisor programs with supervisorctl

  • To see the available programs that supervisor manages we need to run the below command
    sudo supervisorctl
  • To start the supervisor program run the below command
    sudo supervisorctl start <program1> <program2>
  • To stop the supervisor program run the below command
    sudo supervisorctl stop <program1> <program2>
  • To restart the supervisor program run the below command
    sudo supervisorctl restart <program1> <program2>
  • To restart all programs at once then run the below command
    sudo supervisorctl restart all
  • To stop all programs at once then run the below command
    sudo supervisorctl stop all

In web development supervisor is mostly used wsgi processes like uWSGI, gunicorn, etc.

Reference: http://supervisord.org/

 

Read Next: class based views in Django Rest Framework

Read Prev: how to use static files in django

Blog Archive