I am currently using Monit to monitor Apache and restart it if its memory usage is too high. However, I'd also like to be able to monitor the individual apache2 subprocesses that are spawned, and kill any subprocess whose memory usage is too high over a period of a few minutes. How can I do that?
Monit's documentation suggests that you can natively monitor the total memory used by Apahce and its child processes, not any individual child process.
However, you can check the return status of a script using the
check program
test:http://mmonit.com/monit/documentation/monit.html#program_status_testing
So, you can do something like this as a check script:
If that script is
/usr/local/bin/check_apache_children.sh
, then you can do something like:The kill script will presumably look like the check script, but with a kill on the PID instead of an exit.
The scripts are, of course, illustrative, and should be modified to your environment.
I accepted cjc's answer above, but wanted to post exactly how I used his suggestion to solve this problem. Note that you will need to use at least Monit 5.3 to use Monit's "check program". I am running Debian.
/usr/local/bin/monit_check_apache2_children:
/etc/monitrc: