If you are working with long-running processes on web servers and struggling hard to manage them easily, supervisord process monitoring software can help you the most. I have recently learned about it and can’t help but share it with my readers about this excellent piece of software. I will try to introduce this to you and show you the primary usage, which is basically intended for beginners. If you already know about it, you may skip this article and surf around this site for other valuable articles :).
What is SupervisorD?
The name fits with its functionality. It controls/manages processes for you. Besides performing basic functionality, it also acts as a server interface to listen to clients(plugins) and perform operations based on request type(terminate, restart, stop, etc.).
Why Supervisord Process Monitoring?
In the case of a system’s resource-killing software processes or your own built custom processes application, it could go wrong anytime. Now, as long as we are sitting in front of a pc and we can restart/terminate the process if overloading the server, we would be just fine, but manually doing so isn’t an efficient idea at all. Here comes a supervisor to get you covered. It automates the system of monitoring long-running processes and, by default, restarts automatically if a process crashes somehow and stuff like that.
Download/Install Supervisord:
Supervisord is available on a UNIX-like operating system. It’s usually installed with the Python package manager ‘easy_install’ or ‘pip.’ So, as long as you have Python working on your system, it should work just fine for you. So, basically, you can install it by running any of the following commands:
pip install supervisor
#or
easy_install install supervisor
Code language: CSS (css)
The official site has more detailed documentation for installation
Supervisor Configuration:
The configuration file, usually named ‘supervisord.conf’, located at “/etc/supervisord.conf” path, is the most critical section that we will have to interact with most of the time. This is the place where we will set all processes to monitor as well. It’s good practice to get that part after the ‘[program:theprogramname]’ titled section, as this is the same configuration and given as an example.
This file also includes event listeners, which are third-party plugin. Supervisord will send regular updates to those plugins based on a certain event occurrence or in regular time interval so that plugins can analyses the scenario and acts as necessary.
Running Your First Process:
There is already a sample example for the program, which is the ‘cat’ program. To get it enabled, you just have to remove the comment on the two lines in the program section so that it looks like the below:
[program:theprogramname]
command=/bin/cat
Code language: JavaScript (javascript)
After it’s done and you have saved the configuration file, run the following command on your terminal:
/usr/bin/supervisord -n
Here, -n is to tell the program to run directly, not as a daemon, so that you can check the log as stdout. If all goes OK, you should see something like below:
2014-06-25 05:31:50,655 INFO RPC interface 'supervisor' initialized
2014-06-25 05:31:50,688 INFO supervisord started with pid 1587
2014-06-25 05:31:51,692 INFO spawned: 'theprogramname' with pid 1590
Code language: JavaScript (javascript)
So, it means the supervisor has started running the program for you. Congratulation!
Running a custom script:
Let’s say we have a custom script written in Python or PHP that does some background tasks. To perform in real-time, they need to be run all the time so that they can respond as soon as a task arrives. So, using a cronjob isn’t a good idea. To configure such a program with supervisord, add the following example instruction in the configuration file:
[program:background-worker-1]
command=/usr/bin/php /path/to/background_worker.php {agr1} {arg2}
[program:background-worker-2]
command=/usr/bin/python /path/to/background_worker.py {agr1} {arg2}
Code language: JavaScript (javascript)
And we should be just fine. Now, as long as you are supervisory, it will run your workers as a process, and it will always keep running! If your program exits/crashes for some reason, it will automatically start again!
What’s Next?
I hope that this will be enough for a beginner to understand and have a quick start. If you are enjoying and getting your hands dirty, you will also be interested in learning about supervisord web interface as well. If you have any specific questions/suggestions to add something to this tutorial, please leave them in the comments. Keep in touch!
Divya says
Hello,
I am trying to figure out a way by which I can use supervisorD to monitor processes that are in multiple servers. I have looked up everywhere for some configuration examples but could not find any information.
Any advice/suggestions will be greatly appreciated.
You will have to have supervisord installed on all of those servers. However, Sure you can monitor them from a single web interface, which you can install anywhere you want. Checkout all the web interface plugins list here: http://supervisord.org/plugins.html#dashboards-and-tools-for-multiple-supervisor-instances. You can checkout my other supervisord web interface tutorial as well.
Thank you for your response. I actually was looking for more information on how to get the third party plugins to get to use for my purpose. I have no clue on how to work on those plugins.
Thank you for your blog post, very helpful!
Thanks a lot for this 🙂