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 statusTo start a program (where program-name is the name of the service):
supervisorctl start program-nameTo stop a program (where program-name is the name of the service):
supervisorctl stop program-nameStep 1 - Install a text editor
Install nano editor on your system:
sudo apt-get update && sudo apt-get install nanoStep 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.confText 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 rereadYou should get something like:
root@wiki:/# supervisorctl reread
cron: available Tell supervisord to load and start the new program:
supervisorctl updateYou should get something like:
root@wiki:/# supervisorctl update
cron: added process groupStep 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
-fwe used oncron. - 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.
