15

I like using monit for the web interface where I can see running monitored processes. I recently upgraded a server to Ubuntu 16.04 and it is using systemd.

I have everything else running but I cannot find the right monit commands to control systemd...

I used to do /etc/init.d/process start or stop or whatever. That obivously no longer works so I tried ...

systemctl start process which didn't work either. What can I do here? My monitrc is pasted below...(old style which worked on 14.04)

check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop program = "/etc/init.d/nginx stop"

check process sshd with pidfile /var/run/sshd.pid
    start program = "etc/init.d/ssh start"
    stop program = "etc/init.d/ssh stop"
Joff
  • 383
  • 1
  • 3
  • 13

3 Answers3

15

This should work on RHEL / CentOS 7 / Ubuntu 18.04

check process nginx with pidfile /var/run/nginx.pid
    start program = "/bin/systemctl start nginx"
    stop program = "/bin/systemctl stop nginx"

check process sshd with pidfile /var/run/sshd.pid
    start program = "/bin/systemctl start sshd"
    stop program = "/bin/systemctl stop sshd"
Roger Lipscombe
  • 1,670
  • 1
  • 20
  • 34
vjjj
  • 251
  • 2
  • 4
4

Monit expects all paths to be fully qualified. It is clearly the Monit Control File (/etc/monit/monitrc).

In this case fully qualified path for systemctl is required to be specified.

On Ubuntu 20.04 LTS, I used which systemctl to find its fully qualified path which was /usr/sbin/systemctl.

So, below given snippet worked for me.

start program = "/usr/sbin/systemctl start nginx"
stop program = "/usr/sbin/systemctl stop nginx"
0

on Ubuntu 16 or later, you can using systemctl then start or stop services:

check process nginx with pidfile /var/run/nginx.pid
    start program = "systemctl start nginx"
    stop program = "systemctl stop nginx"

check process sshd with pidfile /var/run/sshd.pid
    start program = "systemctl start sshd"
    stop program = "systemctl stop sshd"
NamNT
  • 9
  • 1
  • that does not work, I get an alert that says "Description: failed to start (exit status -1) -- Program systemctl failed: File 'systemctl' does not exist" – Joff Nov 18 '16 at 06:32
  • and if I use absolute paths I get alerts that stop action was done, but in my ssh session it says it is still running and my syslog says it did nothing – Joff Nov 18 '16 at 06:41
  • 3
    @deltaskelta You need to give the full path, `/bin/systemctl` – Sharn Jayantha Oct 15 '17 at 09:40