avatarMax Zhang

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5972

Abstract

lass="hljs-attr">autorestart</span>=<span class="hljs-literal">true</span> <span class="hljs-attr">stderr_logfile</span>=/var/log/idle.err.log <span class="hljs-attr">stdout_logfile</span>=/var/log/idle.out.log</pre></div><p id="5394">We’ll review this line by line:</p><div id="5bb2"><pre><span class="hljs-built_in">command</span>=/home/ubuntu/idle.sh</pre></div><p id="c50d">The configuration begins by defining a program with the name <code>idle</code> and the full path to the program:</p><div id="4c7b"><pre><span class="hljs-attr">autostart</span>=<span class="hljs-literal">true</span> <span class="hljs-attr">autorestart</span>=<span class="hljs-literal">true</span></pre></div><p id="c45c">The next two lines define the automatic behavior of the script under certain conditions.</p><p id="01dc">The <code>autostart</code> option tells Supervisor that this program should be started when the system boots. Setting this to false will require a manual start following any system shutdown.</p><p id="ab89"><code>autorestart</code> defines how Supervisor should manage the program in the event that it exits:</p><ul><li><code>false</code> tells Supervisor not to ever restart the program after it exits.</li><li><code>true</code> tells Supervisor to always restart the program after it exits.</li><li><code>unexpected</code> tells Supervisor to only restart the program if it exits with an unexpected error code (by default anything other than codes 0 or 2). To learn more about error codes, look into the <code>errno</code> command.</li></ul><div id="de90"><pre><span class="hljs-attr">stderr_logfile</span>=/var/log/idle.err.log <span class="hljs-attr">stdout_logfile</span>=/var/log/idle.out.log</pre></div><p id="023c">The final two lines define the locations of the two main log files for the program. As suggested by the option names, stdout and stderr will be directed to the <code>stdout_logfile</code> and <code>stderr_logfile</code> locations respectively. The specified directories must already exist, as Supervisor will not attempt to create any missing directories.</p><p id="634a">The configuration we have created here is a minimal template for a Supervisor program. <a href="http://supervisord.org/configuration.html#program-x-section-settings">The Supervisor documentation</a> lists many more optional configuration options that are available to tune how programs are run.</p><p id="de47">Once our configuration file is created and saved, we can inform Supervisor of our new program through the <code>supervisorctl</code> command. First we tell Supervisor to look for any new or changed program configurations in the <code>/etc/supervisor/conf.d</code> directory with:</p><div id="3c7b"><pre><span class="hljs-variable"> </span>sudo supervisorctl reread</pre></div><div id="39ef"><pre><span class="hljs-section">idle: available</span></pre></div><p id="5e44">Followed by telling it to enact any changes with:</p><div id="abbb"><pre><span class="hljs-variable"> </span>sudo supervisorctl update</pre></div><div id="b071"><pre><span class="hljs-symbol">idle:</span> added process <span class="hljs-keyword">group</span></pre></div><p id="89b3">Any time you make a change to any program configuration file, running the two previous commands will bring the changes into effect.</p><p id="316e">At this point our program should now be running. We can check its output by looking at the output log file:</p><div id="431c"><pre><span class="hljs-meta prompt_">$ </span><span class="language-bash">sudo <span class="hljs-built_in">tail</span> /var/log/idle.out.log</span></pre></div><div id="530e"><pre><span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:22</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:23</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:24</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:25</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:26</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:27</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:28</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:29</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:30</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="

Options

hljs-string">Sat</span> <span class="hljs-string">Nov</span> <span class="hljs-number">20</span> <span class="hljs-number">22</span><span class="hljs-string">:21:31</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span></pre></div><p id="c296">Next, we’ll cover some other usage of Supervisor.</p><h1 id="c5ff">Step 3 — Managing Programs</h1><p id="a493">Beyond running programs, you will want to stop, restart, or see their status. The supervisorctl program, which we used in <i>Step 2</i>, also has an interactive mode which we can use to control our programs.</p><p id="f808">To enter the interactive mode, run supervisorctl with no arguments:</p><div id="8a40"><pre><span class="hljs-variable"> </span>sudo supervisorctl</pre></div><div id="ecb6"><pre>idle RUNNING pid 12614, <span class="hljs-built_in">uptime</span> 1:49:37 supervisor&gt;</pre></div><p id="31c7"><code>supervisorctl</code> will initially print the status and uptime of all configured programs, followed by its command prompt. Entering <code>help</code> will reveal all of its available commands:</p><div id="86c0"><pre><span class="hljs-meta prompt_"> </span><span class="language-bash">supervisor> <span class="hljs-built_in">help</span></span></pre></div><div id="a7a3"><pre>default commands (<span class="hljs-built_in">type</span> <span class="hljs-built_in">help</span> <topic>):

add clear <span class="hljs-built_in">fg</span> open quit remove restart start stop update avail <span class="hljs-built_in">exit</span> maintail pid reload reread shutdown status <span class="hljs-built_in">tail</span> version</pre></div><p id="5435">You can <code>start</code> or <code>stop</code> a program with the associated commands followed by the program name:</p><div id="c137"><pre><span class="hljs-variable"> </span>supervisor&gt; stop idle</pre></div><div id="6a56"><pre><span class="hljs-section">idle: stopped</span></pre></div><div id="ba65"><pre><span class="hljs-variable"> </span>supervisor> start idle</pre></div><div id="d0b5"><pre><span class="hljs-section">idle: started</span></pre></div><p id="6716">Using the <code>tail</code> command, you can view the most recent entries in the stdout and stderr logs for your program:</p><div id="ce97"><pre><span class="hljs-meta prompt_"> </span><span class="language-bash">supervisor&gt; <span class="hljs-built_in">tail</span> idle</span></pre></div><div id="9cbe"><pre><span class="hljs-string">Sun</span> <span class="hljs-string">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">00</span><span class="hljs-string">:36:10</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sun</span> <span class="hljs-string">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">00</span><span class="hljs-string">:36:11</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sun</span> <span class="hljs-string">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">00</span><span class="hljs-string">:36:12</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sun</span> <span class="hljs-string">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">00</span><span class="hljs-string">:36:13</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sun</span> <span class="hljs-string">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">00</span><span class="hljs-string">:36:14</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sun</span> <span class="hljs-string">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">00</span><span class="hljs-string">:36:15</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span> <span class="hljs-string">Sun</span> <span class="hljs-string">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">00</span><span class="hljs-string">:36:17</span> <span class="hljs-string">UTC</span> <span class="hljs-number">2021</span></pre></div><div id="c70e"><pre><span class="hljs-meta prompt_"> </span><span class="language-bash">supervisor> <span class="hljs-built_in">tail</span> idle stderr</span></pre></div><div id="6c42"><pre><span class="hljs-type">error</span>! <span class="hljs-type">error</span>! <span class="hljs-type">error</span>! <span class="hljs-type">error</span>! <span class="hljs-type">error</span>! <span class="hljs-type">error</span>! <span class="hljs-type">error</span>!</pre></div><p id="01a1">Using <code>status</code> you can view again the current execution state of each program after making any changes:</p><div id="d984"><pre> supervisor&gt; <span class="hljs-built_in">status</span></pre></div><div id="4fb0"><pre>idle <span class="hljs-variable constant_">STOPPED</span> <span class="hljs-title class_">Nov</span> <span class="hljs-number">21</span> <span class="hljs-number">01</span>:<span class="hljs-number">07</span> <span class="hljs-variable constant_">AM</span></pre></div><p id="559e">Finally, you can exit supervisorctl with Ctrl+C or by entering <code>quit</code> into the prompt:</p><div id="c3d7"><pre><span class="hljs-variable"> </span>supervisor> quit</pre></div><h1 id="ba28">Conclusion</h1><p id="5cce">In this tutorial, you learned how to install and manage Supervisor. As mentioned, Supervisor is very lightweight by modern standards, but it continues to be well-maintained, and it can be a useful tool for smaller deployments. It is also a low-maintenance and self-contained way of generating logs as a component part of a larger deployment.</p></article></body>

How to install and manage supervisor on ubuntu and debian

Introduction

In many VPS environments, it is often the case that you will have a number of small programs that you want to run persistently, whether these are small shell scripts, Node.js apps, or any large-sized packages.

Usually, external packages are supplied with a unit file that allows them to be managed by an init system such as systemd, or packaged as docker images which can be managed by a container engine. However, for software that isn’t well-packaged, or for users who would prefer not to interact with a low-level init system on their server, it is helpful to have a lightweight alternative.

Supervisor is a process manager which provides a singular interface for managing and monitoring a number of long-running programs. In this tutorial, you will install Supervisor on a Linux server and learn how to manage Supervisor configurations for multiple applications.

Prerequisites

To complete this guide, you will need:

  • An Linux server and a non-root user with sudo privileges.

Step 1 — Installation

Begin by updating your package sources and installing Supervisor:

$ sudo apt update && sudo apt install supervisor

The supervisor service runs automatically after installation. You can check its status:

$ sudo systemctl status supervisor

You should receive the following output:

 supervisor.service - Supervisor process control system for UNIX
     Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-11-17 22:56:48 UTC; 5min ago

Now that we have Supervisor installed, we can look at adding our first programs.

Step 2 — Adding a Program

A best practice for working with Supervisor is to write a configuration file for every program it will handle.

All programs run under Supervisor must be run in a non-daemonising mode (sometimes also called ‘foreground mode’). If, by default, your program automatically returns to the shell after running, then you may need to consult the program’s manual to find the option to enable this mode, or Supervisor will not be able to properly determine the status of the program.

In order to demonstrate Supervisor’s functionality, we’ll create a shell script that does nothing other than produce some predictable output once a second, but will run continuously in the background until it is manually stopped. Using nano or your favorite text editor, open a file called idle.sh in your home directory:

$ nano ~/idle.sh

Add the following contents:

~/idle.sh

#!/bin/bash
while true
do 
	# Echo current date to stdout
	echo `date`
	# Echo 'error!' to stderr
	echo 'error!' >&2
	sleep 1
done

Save and close the file. If you are using nano, press Ctrl+X, then when prompted, Y and Enter.

Next, make your script executable:

$ chmod +x ~/idle.sh

The per-program configuration files for Supervisor programs are located in the /etc/supervisor/conf.d directory, typically running one program per file and ending in .conf. We’ll create a configuration file for this script, as`/etc/supervisor/conf.d/idle.conf:

$ sudo nano /etc/supervisor/conf.d/idle.conf

Add these contents:

/etc/supervisor/conf.d/idle.conf

command=/home/ubuntu/idle.sh
autostart=true
autorestart=true
stderr_logfile=/var/log/idle.err.log
stdout_logfile=/var/log/idle.out.log

We’ll review this line by line:

command=/home/ubuntu/idle.sh

The configuration begins by defining a program with the name idle and the full path to the program:

autostart=true
autorestart=true

The next two lines define the automatic behavior of the script under certain conditions.

The autostart option tells Supervisor that this program should be started when the system boots. Setting this to false will require a manual start following any system shutdown.

autorestart defines how Supervisor should manage the program in the event that it exits:

  • false tells Supervisor not to ever restart the program after it exits.
  • true tells Supervisor to always restart the program after it exits.
  • unexpected tells Supervisor to only restart the program if it exits with an unexpected error code (by default anything other than codes 0 or 2). To learn more about error codes, look into the errno command.
stderr_logfile=/var/log/idle.err.log
stdout_logfile=/var/log/idle.out.log

The final two lines define the locations of the two main log files for the program. As suggested by the option names, stdout and stderr will be directed to the stdout_logfile and stderr_logfile locations respectively. The specified directories must already exist, as Supervisor will not attempt to create any missing directories.

The configuration we have created here is a minimal template for a Supervisor program. The Supervisor documentation lists many more optional configuration options that are available to tune how programs are run.

Once our configuration file is created and saved, we can inform Supervisor of our new program through the supervisorctl command. First we tell Supervisor to look for any new or changed program configurations in the /etc/supervisor/conf.d directory with:

$ sudo supervisorctl reread
idle: available

Followed by telling it to enact any changes with:

$ sudo supervisorctl update
idle: added process group

Any time you make a change to any program configuration file, running the two previous commands will bring the changes into effect.

At this point our program should now be running. We can check its output by looking at the output log file:

$ sudo tail /var/log/idle.out.log
Sat Nov 20 22:21:22 UTC 2021
Sat Nov 20 22:21:23 UTC 2021
Sat Nov 20 22:21:24 UTC 2021
Sat Nov 20 22:21:25 UTC 2021
Sat Nov 20 22:21:26 UTC 2021
Sat Nov 20 22:21:27 UTC 2021
Sat Nov 20 22:21:28 UTC 2021
Sat Nov 20 22:21:29 UTC 2021
Sat Nov 20 22:21:30 UTC 2021
Sat Nov 20 22:21:31 UTC 2021

Next, we’ll cover some other usage of Supervisor.

Step 3 — Managing Programs

Beyond running programs, you will want to stop, restart, or see their status. The supervisorctl program, which we used in Step 2, also has an interactive mode which we can use to control our programs.

To enter the interactive mode, run supervisorctl with no arguments:

$ sudo supervisorctl
idle                      RUNNING    pid 12614, uptime 1:49:37
supervisor>

supervisorctl will initially print the status and uptime of all configured programs, followed by its command prompt. Entering help will reveal all of its available commands:

$ supervisor> help
default commands (type help <topic>):
=====================================
add    clear  fg        open  quit    remove  restart   start   stop  update
avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version

You can start or stop a program with the associated commands followed by the program name:

$ supervisor> stop idle
idle: stopped
$ supervisor> start idle
idle: started

Using the tail command, you can view the most recent entries in the stdout and stderr logs for your program:

$ supervisor> tail idle
Sun Nov 21 00:36:10 UTC 2021
Sun Nov 21 00:36:11 UTC 2021
Sun Nov 21 00:36:12 UTC 2021
Sun Nov 21 00:36:13 UTC 2021
Sun Nov 21 00:36:14 UTC 2021
Sun Nov 21 00:36:15 UTC 2021
Sun Nov 21 00:36:17 UTC 2021
$ supervisor> tail idle stderr
error!
error!
error!
error!
error!
error!
error!

Using status you can view again the current execution state of each program after making any changes:

$ supervisor> status
idle                      STOPPED    Nov 21 01:07 AM

Finally, you can exit supervisorctl with Ctrl+C or by entering quit into the prompt:

$ supervisor> quit

Conclusion

In this tutorial, you learned how to install and manage Supervisor. As mentioned, Supervisor is very lightweight by modern standards, but it continues to be well-maintained, and it can be a useful tool for smaller deployments. It is also a low-maintenance and self-contained way of generating logs as a component part of a larger deployment.

Management
Installation
Supervisor
Ubuntu
Debian
Recommended from ReadMedium