Posted on January 25, 2017 by Abhirath Mahipal
In this post I’ll show you how to setup Supervisor on Linux and become familiar with a few handy options and settings. If you haven’t used Supervisor in the past this post should give you a decent start.
Supervisor is a Python program that makes managing other programs (processes) a breeze. It can ease the process of starting, killing and restarting applications that you develop. In this post I’ll explain various aspects that you need to keep in mind while using Supervisor.
.confin certain directories. It then follows the instructions that you’ve given in those files.
.conffile is written starting any program is as simple as
supervisorctl start appname.
On Ubuntu or debian you can
sudo apt-get install supervisor
You can also install it using pip. Supervisor on Pypi.
Supervisor can be informed of any program that you wish to run via a
I found this example from Digital Ocean to be very helpful. So I’m adapting a similar approach but with a different script. For the sake of this example and brevity we’ll write a small script that displays the amount of RAM that the machine sports.
Go to your home directory (~/) and save the following as
#!/bin/bash counter=0 while true do ((counter++)) echo "Count is now $counter." cat /proc/meminfo | grep MemTotal sleep 1 done
Make the file you just created executable.
chmod +x displayram.sh
Save the following as
displayram.conf. Supervisor will use this file to control the process. Substitute abhirath with your username.
[program:displayram] command=/home/abhirath/displayram.sh autostart=false autorestart=false stderr_logfile=NONE stdout_logfile=/home/abhirath/displayram.out.log
Move the file that you just created to
/etc/supervisor/conf.d/displayram.conf. This is the default directory that Supervisor looks for programs.
Then on the terminal
supervisorctl reread followed by
On a terminal enter
supervisorctl start displayram. Our script starts instantly.
You can verify that it works by opening
/home/yourusername/displayram.out.log. It should be filled with lines displaying the RAM installed on the machine.
A better way would be to run
tail -f /home/yourusername/displayram.out.log
on a terminal. It will keep printing the latest entries in the file.
You will also need to familiarise yourself to spawn more than one process - An example which requires running multiple processes to start an app.
To stop the script we just started run
supervisorctl stop displayram.
displayramexample uses absolute paths. If you want to use relative paths you need to set the path. You can read more about it here.
bash -c "command here". Our earlier example can be rewritten as
[program=displayram] directory=/home/abhirath command=bash -c "./displayram" autostart=false autorestart=false stderr_logfile=NONE stdout_logfile=/home/abhirath/displayram.out.log
supervisorctl stop programname
stderr_logfile- the file to which it shall redirect stderr to. If set to NONE it will not log output from stderr.
stdout_logfile- the file to which it redirect stdout to. This can also be set to NONE.
stderr_logfile_maxbytes- maximum space that the log file will take before Supervisor uses a new file to store logs. You can input a numerical followed by GB, KB etc. By default it’s set to 50MB.
stdout_logfile_maxbytes- same as above but for stdout.
stdout_logfile_backups- the number of backup files it stores. Say you’ve set it to 3. After the first files exceeds the value of
stdout_logfile_maxbytesit creates a second file then a third. If the third file exceeds the size it proceeds to delete the first file and continue logging. In all it would use a maximum of 3 * 50MB in our case. By default it has a value of 10.
stderr_logfile_backups- same as above but for stderr.
Supervisor saves it’s own logs as well. They are different from the logs you set for your program. These logs contain information about Supervisor. They are stored in
To see the last few logs you can
A useful read - Options available under program.
unix:///var/run...sock no such file?
It means Supervisor isn’t running. You will have to start Supervisor.
sudo service supervisor restart
Spawn errors. Supervisord is unable to start your program?
tail -f /path/to/program/log/fileto keep track of changes happening.
directoryoptions under your program. Give absolute paths like
Supervisor doesn’t start automatically after bootup?
.conf files that you write are not recognised. If that is the case do the following:-
Check the syntax of everything in the file. If there’s an error Supervisor doesn’t even show the file. Also the files must have an extension of
Any signs of the process / app that you want to start is already running?
supervisorctl appname status.
killasgroupwork on individual programs. They are not to be confused with group applications