Supervisor is a great tool to start and manage long-running processes and the log files associated with them. It offers a lot of helpful features and is easy to get up and running.
Supervisor works out of the box, so this part is easy.
Supervisor looks for configuration files in /etc/supervisor/conf.d/ and it is a good idea to keep a single conf file per process. To start, let’s set up a simple long-running python script. Don’t pay too much attention to the contents of the conf file, we’ll go over that later.
Create /etc/supervisor/conf.d/test_python.conf containing:
Create ~/test.py containing:
When creating the conf file, be sure to replace /home/ubuntu both times with the path to your home directory (~ isn’t allowed). If you run test.py manually you’ll see it prints out the time once per second.
SupervisorCTL is the tool you will use to manage everything in Supervisor. Start by running sudo supervisorctl and then typing reread. The reread command goes through the conf.d directory and loads any new program conf files. Once you see the new test_python program available, you can use the add command to load and start it (add test_python). The status command shows you all running processes, their PID and how long they’ve been running. Here is a short list of my commonly used supervisorctl commands and what they do.
|reread||Reload all program conf files from the conf.d directory.|
|add [program_name]||Add a newly created conf file to Supervisor and start the process.|
|status||Check the status of all programs currently managed by Supervisor.|
|start [program_name]||Start the given program. Used often with one-time scripts.|
|restart [program_name]||Restart the given program.|
|tail -f [program_name]||Watch the log file in real-time (same as UNIX tail -f [filename]).|
|help||List all available commands.|
Exit supervisorctl and check your log file (~/test_python_output.txt) to see that it is still running. If it isn’t, you may have missed a step.
There is a wide array of options you can use while setting up your program configuration file. I’ve listed my favorite options below. For a full list of commands and their parameters, check out the settings documentation
|command||The command to run.|
|directory||The directory to run the command in.|
|numprocs||Automatically start N instances of that process.|
|autostart||A boolean which tells whether to run automatically or not when Supervisor starts.|
|autorestart||Automatically restart a process that dies, under specific conditions (see docs).|
|stdout_logfile||Where to log the output of your program.|
|redirect_stderr||Tell supervisor whether to include stderr in your standard output file.|
|stdout_logfile_maxbytes||The size a log file is allowed to grow to until it is auto rotated.|
|stdout_logfile_backups||The number of rotated log files to keep.|
Supervisor provides a really nifty web interface to monitor and restart your processes. Just update the following file, restart supervisor, and you’ll see it on port 9001. Feel free to change the port and protect it using HTTP Auth or whatever you see fit.
Add the following to /etc/supervisor/supervisord.conf
I’ve found that Supervisor also works great for running commonly used scripts that have multiple parameters or large log files. Be sure to set autostart and autorestart in the conf file to false, so your script doesn’t end up running repeatedly.