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.
sudo apt-get install supervisor -y && sudo supervisord
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:
[program:test_python] command=python -u test.py directory=/home/ubuntu stdout_logfile=/home/ubuntu/test_python_output.txt redirect_stderr=trueCreate ~/test.py containing:
import time while True: print(time.ctime()) time.sleep(1)
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.
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.
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.
[program:mongod_member] command=/home/ubuntu/mongodb-linux-x86_64-2.0.6/bin/mongod --dbpath /mnt/mongo/ --replSet mongo5 --nojournal directory=/home/ubuntu stdout_logfile=/mnt/log/mongod.log redirect_stderr=true