Managing Custom Scripts With Supervisor
One thing that becomes quite commonplace when managing servers is the need to write scripts to automate tasks. Oftentimes these scripts are intended to aid in monitoring services on the server, or may even be the services that the server provides. The problem is that unless your scripts are designed to be restarted over and over again, then managing them can be awkward. Creating init scripts for scripts that need to run as a daemon could be awkward, and as init services have moved on to systemd it means that everything needs to be changed for the new system. Fortunately, supervisor exists with the plan to resolve these problems by making daemon management for scripts much easier.
What is Supervisor?
Supervisor doesn’t aim to replace systemd or any other init system, rather it’s intended to complement the existing init system on the server and run as a daemon itself managing the scripts that you want to have running. Once it starts at bootup it will automatically run any scripts you’ve configured it to start; it can also be set to restart scripts if they fail for any reason. There’s no need to worry about designing your script to log to files for you after testing as Supervisor can be configured to redirect any output or errors from the script to log files. It also makes it very simple to run a script as a particular user that you may wish for it to run as, rather than leaving it running as root. So if all that sounds appealing, let’s look at getting it installed and how to configure it.
Installing Supervisor
Fortunately, installation is fairly simple as supervisor comes with the default repositories for Debian and Ubuntu and in the EPEL repositories for CentOS and Red Hat. To install in Debian and Ubuntu use:
sudo apt-get update
sudo apt-get install supervisor
For CentOS and Red Hat systems you’ll first need to configure the EPEL repositories from the Fedora project here: http://fedoraproject.org/wiki/EPEL. Once installed, you can then install supervisor with the following command:
yum install supervisor
Annoyingly, depending on the distribution you use, the configuration is stored slightly differently. On Debian and Ubuntu systems the main configuration is stored in /etc/supervisor/supervisord.conf and configuration for your individual scripts is stored in the /etc/supervisor/conf.d directory in files ending with the “.conf” file extension. On CentOS and Red Hat, the main configuration is in /etc/supervisord.conf, and any additional configuration goes into the /etc/supervisord.d directory in files ending with the “.ini” file extension. Despite these differences, the contents of the configuration files themselves are identical, regardless of the distribution you are using it on. So let’s look at an example configuration that you might put in one of those files:
[program:myScript]
command=/root/scripts/myscript.sh
directory=/root/scripts
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/myscript-error.log
stdout_logfile=/var/log/myscript.log
user=scripty
environment=USEFUL_THING=’something useful’
A Look At The Configuration File
- The first line tells Supervisor the name you’ll be using to refer to your program. In this case we’ve set it to “myScript”, but this can be pretty much anything you want.
- The next line is the command line, telling Supervisor the command it needs to run.
- Then we have the directory line which tells Supervisor which directory should be the working directory for your script to work.
- This is followed by the autostart setting. When set to “true” the program will be automatically started at bootup, and when set to “false”, it won’t be.
- The autorestart line determines whether Supervisor should automatically restart the program should it stop running. Again “true” means it will try to auto restart, “false” means it won’t.
- Related to the above startretries is the number of attempts that Supervisor will make to try and start a program that has stopped running before it gives up and assumes it is broken.
- The stderr_logfile and stdout_logfile dictates the files in which supervisor should save output from both the stderr output (Error output) and stdout output (Normal user output you’d see when running the program manually).
- The user line allows you to provide a user that you’d like your script running as.
- Finally, the environment line allows you to specify any environment variables that may need to be set for your script to run successfully
That’s a pretty simple way to control your scripts. You’ll need to make a file like that for each of the scripts you’ll want to run. To get it started, run the following commands:
sudo supervisorctl reread
sudo supervisorctl update
This will get Supervisor to reload its configuration. Once that is done, get it to update the processes it is running based off of the new configuration
If it hasn’t started your script already then you can manually start it with:
sudo supervisorctl start myScript
In this case, changing “myScript” for the name you gave your program in the configuration file.