Ask Your Question

Can systemd replace monit?

asked 2014-04-11 04:26:49 -0500

q2dg gravatar image

Essentially monit is a monitoring tool, which you configure tests that will be evaluated at certain intervals. If one of the tests fails, then it will take action depending on the rules you assign to it. Monit can monitor processes and restart them when required. It’s possible to restart a process based on CPU usage or memory, for instance.

Can I do the same only with Systemd? If so, how could it be done?


edit retag flag offensive close merge delete



You might also consider managing the resources available to your units with systemd. It won't directly mail you if, say, httpd memory consumption gets over 1GB, but you can configure it to restrict the memory available to httpd and all child processes to 1GB. The magic happens using cgroups, read more at

randomuser gravatar imagerandomuser ( 2014-04-23 10:39:40 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2014-04-14 14:09:46 -0500

marcindulak gravatar image

updated 2014-04-23 08:34:53 -0500

Let me start by saying that in my opinion restarting any service automatically is a bad idea.

Answering partly your question - it is possible to design a primitive monitoring system based on systemd. I have never used monit, but the number of available nagios plugins ( ) is something hard to beat. Therefore the example below uses a nagios plugin from within systemd, which in turn sends emails in case the check performed by the plugin fails.

Example below for checking for httpd:

  1. install the nagios plugin (and httpd to be monitored)

    su -c "yum -y install httpd"
    su -c "yum -y install nagios-plugins-http"
  2. configure systemd mailer:

    su -c "yum -y install sendmail-cf"

    Configure sendmail with e.g. SMART_HOST , see

    All steps as root from now.

    The systemd script (idea taken from ) reads:

    cat <<'EOF' > /root/mail
    (echo "Subject: Failed Service: ${1} on `hostname`"; /usr/bin/systemctl status "${1}.service") | /usr/sbin/sendmail
    chmod go-rwx /root/mail

    /usr/bin/mail did not work for me. The above script is executed by the following systemd service (for usage of the systemd specifiers (@, %i, etc), see ):

    cat <<'EOF' > /usr/lib/systemd/system/mail@.service
    ExecStart=/usr/bin/sh /root/mail %i
  3. configure the systemd timer service - this will periodically call the systemd service performing the actual nagios check. The timer uses OnCalendar systemd option ( ) and is based on the ideas from and . It will run every minute (haven't figured out how to set more complex time patterns yet - see ):

    cat <<'EOF' > /usr/lib/systemd/system/min@.timer
    Description=Run %i every minute
  4. configure the actual systemd service performing the nagios check_http check and notifying the min@.timer through OnFailure:

    cat <<'EOF' > /usr/lib/systemd/system/check_http.service
    ExecStart=/usr/lib64/nagios/plugins/check_http -H localhost -p 80

Now, how to use those? The main controller is the timer, so:

su -c "systemctl start min@check_http.timer"

You should start receiving emails now. Let's start apache:

su -c "systemctl start httpd.service"

The emails with errors still coming? This is due to the way nagios check_http behaves - one has to create some web contents:

su -c "touch /var/www/html/index.html"

In order to disable the check:

su -c "systemctl stop min@check_http.timer"

or to have it permanently after reboot:

su -c "systemctl enable min@check_http.timer"

You may notice that there are ... (more)

edit flag offensive delete link more


Ouououououooooooh, what an answer!!! Thanks!!

q2dg gravatar imageq2dg ( 2014-04-15 10:14:24 -0500 )edit

Question Tools



Asked: 2014-04-11 04:26:49 -0500

Seen: 7,824 times

Last updated: Apr 23 '14