Install Supervisor and manage process in Ubuntu 18


SystemMen - How to install Supervisor and manage process in Ubuntu 18? This article, I will guide you to use a tool called Supervisor.

This tool allows you to manage processes in Linux. Back in my previous article on MkDocs, you can see that when we run the mkdocs serve command, the process only works at the command line.

So what if I exit the command line window? The process will stop working, we need it to work 24/7. So how?

At this point, we will need a tool that allows us to manage processes in Linux and that is the Supervisor.

Install Supervisor in Ubuntu 18.04

how-to-install-supervisor-and-manage-process-in-ubuntu-18 Install Supervisor and manage process in Ubuntu 18
Install Supervisor and manage process in Ubuntu 18.

First, we need to install the dependencies.

# apt-get install python-setuptools -y

Next, use the following command to install Supervisor.

# sudo easy_install supervisor

Set up Supervisor service

Once the installation is completed, we need to create conf.d directory to contain the Supervisor’s configuration files.

# sudo mkdir -p /etc/supervisor/conf.d

Run the following command to create a configuration file for the main Supervisor service.

# echo_supervisord_conf > /etc/supervisor/supervisord.conf

You open up /etc/supervisor/supervisord.conf file and find the following paragraph.

;[include]
;files = relative/directory/*.ini

Edit it into below. The purpose of this is to declare the process management configuration files into the conf.d directory, each process being a configuration file.

[include]
files=conf.d/*.conf

And save the configuration file.

Next, you need to create a service file for the Supervisor.

# nano /etc/systemd/system/supervisord.service

Copy the content below into the file and save it.

[Unit]
Description=Supervisor daemon
Documentation=http://supervisord.org
After=network.target

[Service]
ExecStart=/usr/local/bin/supervisord -n -c /etc/supervisor/supervisord.conf
ExecStop=/usr/local/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/local/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
Alias=supervisord.service

Reload the daemon service.

# systemctl daemon-reload

Now start the Supervisor service and check its status.

# systemctl start supervisord.service && systemctl status supervisord.service

Establish mkdocs process manager

In the article about mkdocs, we have the command to run the mkdocs service. Now, we will use the Supervisor to manage that process as a service, you can start, stop, status it.

You create /etc/supervisor/conf.d/supervisor_mkdocs.conf file with the following content.

[program:supervisor_mkdocs]
process_name=mkdocs
command=/home/source/venv/bin/mkdocs serve -a 0.0.0.0:8000
environment=PATH="/home/source/venv"
directory=/home/source/
autostart=true
autorestart=true
stderr_logfile=/var/log/mkdocs/mkdocs.err.log
stdout_logfile=/var/log/mkdocs/mkdocs.out.log

You change the information in accordance with your command and environment.

  • program:supervisor_mkdocs: name your service, in this example is supervisor_mkdocs.
  • process_name: name your process, it’s optional.
  • command: the command you want Supervisor manage.
  • environment: define the environment of the command above in the configuration file. In my example, i run the program in virtualenv, so i have to define it here.
  • directory: the working directory of this service.

Next, create the directory containing the log for the mkdocs service.

# mkdir /var/log/mkdocs

And then, you can use the below command to see if the Supervisor detected a newly configured process.

root@dev:/home# sudo supervisorctl reread
supervisor_mkdocs: available

Then you can update it.

root@dev:/home# supervisorctl update
supervisor_mkdocs: added process group

Or restart the Supervisor service.

# systemctl restart supervisord.service

And now, you can check if the mkdocs service is working with the following command.

root@dev:/home# sudo supervisorctl
supervisor_mkdocs:mkdocs RUNNING pid 3270, uptime 4:27:22
supervisor>

Conclusion

So with this article, you already know how to use the Supervisor tool to manage processes in Linux. It helps you to set up some simple commands or scripts that run realtime as a daemon service.

«« »»