My server has several separate Apache Tomcat instances that each take a lot of time and CPU to start. It is not possible to start all of them at the same time. This would generate too much I/O, each service would take much longer to start and the services might even fail to start because of internal timeouts.
Here is some pseudo code that describes what I want to do. How would I accomplish this with a monitrc file?
check process service01 with pidfile /var/run/service01.pid
start program = "/usr/sbin/service service01 start" with timeout 60 seconds
stop program = "/usr/sbin/service service01 stop"
if does not exist then
wait a random number of seconds (between 2 and 5 minutes)
if the cpu load is < 100% then
start program
else
do nothing (check again in the next cycle)
check process service02 with pidfile /var/run/service02.pid
....
This code block would be repeated for each of the 10 services.
The critical step is the random wait. Otherwise, if the server is idle and no service is running (for example after a 'killall -9 java'), monit would check all services, finds that the cpu load is low right now, and start all services at once.