Control a program with Supervisord
November 24th, 2020, 22:03

Introduction

In this tutorial we are going to use supervisord to control a program as a service on our SSH app. Setting up services with supervisord will also automatically start them when your SSH app starts. Supervisord comes preinstalled in your SSH app (16.04 / 18.04 / 20.04).

In this tutorial, we will suppose that the programs we want to control with supervisord, have been installed and properly configured.

In our example we will add cron on supervisord, so that it will automatically run after restart.


How to manage services with Supervisord

Login into your SSH app and then:

To check status of processes:

supervisorctl status

To start a program (where program-name is the name of the service):

supervisorctl start program-name

To stop a program (where program-name is the name of the service):

supervisorctl stop program-name


Step 1 - Install a text editor

Install nano editor on your system:

sudo apt-get update && sudo apt-get install nano


Step 2 - Create program conf file for supervisord

Create a .conf file for each program you want to control (e.g. for cron create a cron.conf file)

nano /etc/supervisor/conf.d/cron.conf

Text editor will open. Type in:

[program:cron]
command=cron -f 

To exit and save the file, press Ctrl+X, type y and press Enter.


Step 3 - Reload supervisord

Tell supervisord load the changes:

supervisorctl reread

You should get something like:

root@wiki:/# supervisorctl reread
cron: available 

Tell supervisord to load and start the new program:

supervisorctl update

You should get something like:

root@wiki:/# supervisorctl update
cron: added process group


Step 4 - Verify that it is running

You can use supervisorctl status to check if it is running:

root@wiki:/# supervisorctl status
cron RUNNING pid 154, uptime 0:00:14
sshd RUNNING pid 8, uptime 0:03:55 

You can also use ps x:

root@wiki:/# ps x
PID TTY STAT TIME COMMAND
  1  ?  Ss   4:29 /usr/bin/python /usr/bin/supervisord
  8  ?  S    0:00 /usr/sbin/sshd -D
154  ?  S    0:00 cron -f
[...] 


Notes

  • Processes have to run in the foreground, not in the background (daemon). Note the -f we used on cron.
  • Supervisord should not be used to run one-off scripts. It will try to keep the process alive, so it will re-run it after it exits. You could use cron instead.
  • You should avoid making changes on ssh.conf, cause you might lock yourself out of your SSH app.


Conclusion

You have successfully setup your services to run with Supervisord.