I have written few posts on process management in Python - fork, exec, wait, and exit. These are exactly same Python APIs around system APIs provided by Linux/Unix/POSIX. In this post, I am going to talk about putting things together and create a simple process monitor.
Functionality of Simple Process Monitor
- Have the list of processes that need to be monitored in the INI file. Let us call this configuration
- Load the configuration file
- Start the process
- Exec the new program
- Loop #3 and #4 until all the processes are loaded
- Monitor the process (wait). A simple call to "wait" would listen for a child to exit.
- When a child dies, handle it either by restarting the process again or do nothing (depending on how it is configured).
- Go back to #6 and continue the monitoring
It is going to tough to post all the code here. Refer my GITHub for the code. Feel free to extend or write tests
Experience: It is another personal experience on higher productivity claim of Python. I wrote the code in a couple of hours and spent around 2 hours on refactoring. Given my (lack of) experience in Python, Python seems to fulfill its promise.