If you are working with long running process 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 myself from sharing with my readers about this awesome piece of software. I will try to introduce this to you and show you the basic usage, which is basically intended for beginners. If you already know about it already, you may skip this article and surf around this site for other useful articles 🙂 .
What is Supervisor?
The name pretty much 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 of request type(terminate, restart, stop etc).
Why Supervisord Process Monitoring?
In case of system’s resource killing software processes or your own built custom processes application or so, could go wrong anytime. Now, as long as we are sitting in front of a pc and we can restart/terminate process if overloading the server, we would be just fine, but manually doing so isn’t an efficient idea at all. Here comes supervisor to get you covered. It automates the system of monitoring long running processes, and by default, restarts automatically if process crashed somehow and staffs like that.
Download/Install Supervisord:
Supervisord is available on UNIX-like operating system. It’s usually being installed with 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 via running any of the following commands:
pip install supervisor #or easy_install install supervisor
The official site has more detailed documentation for installation
Supervisor Configuration:
Configuration file, usually named ‘supervisord.conf’, located at “/etc/supervisord.conf” path, is the most important 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.Its good practice to get that part after ‘[program:theprogramname]’ titled section as this is 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 program, which is ‘cat’ program. To get it enabled, you just have to remove comment on the two lines on program section, so that it look likes as below:
[program:theprogramname] command=/bin/cat
After its done and you saved the configuration file, run the following command on your terminal:
/usr/bin/supervisord -n
Here, -n is to tell the program run directly, not as 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
So, it means, supervisor has started running the program for you. Congratulation!
Running a custom script:
Lets say, we have a custom script written in Python or PHP, which does some background tasks. To perform in real-time, they need to be run all the time, so that they can respond asap a task arrives. So, using cronjob isn’t a good idea. To configure such program with supervisord, add 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}
And we should be just fine. Now as long as you run supervisord, it will run your workers as process and keep it running always! If your program exits/crashes for some reason, it will automatically start it again!
Final Words:
I hope, for basic understanding and have a quick start, this should be enough for a beginner. If you have any specific questions/suggestions to add something on this tutorial, please leave it in the comment. Keep in touch!
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
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.
Md Ali Ahsan Rana says
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.
divya says
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.
dixon1e says
Thank you for your blog post, very helpful!
Abhirath says
Thanks a lot for this 🙂