Search code examples
monit

Monit - Check if process exists and kill when consuming X memory


I have 'someprocess' running on some hosts, and want a single monit check for hosts with and without 'someprocess' running, which kills someprocess when memory usage exceeds a threshold.

Below check works, but on hosts not running someprocess monit continually logs "process is not running" in /var/log/monit.log.

check process someprocess
    matching "someprocess"
    if memory usage > 2% for 1 cycle then exec "/usr/bin/kill someprocess"

I want to also include 'if exists' but keep getting monit systax errors, Im not sure I can have more than one if statement.

Does anyone know if I can do this, so have something along the lines of:

if exists AND memory usage > 2% for 1 cycle then exec "/usr/bin/kill someprocess"

Solution

  • You can add an additional test and disable the monitoring if the application is not available, to prevent floating the logs with messages.

    check process someprocess matching "someprocess"
      if memory usage > 2% for 1 cycle then exec "/usr/bin/kill someprocess"
      if not exist for 5 cycles then unmonitor
    

    This will disable the check, if the application is not available for 5 monitor cycles. To enable the monitoring again, use "monit monitor someprocess".

    See https://mmonit.com/monit/documentation/monit.html#EXISTENCE-TESTS