6

I am using Debian 9.8

I have a service that runs my dotnet program. I would like to monitor it with monit but in all of the examples, you need to reference a .pid file in /var/run but my dotnet program doesn't have a .pid file in /var/run.

So I added PIDFile=/var/run/testservice.pid to the .service file for my service but it does not create the file when I start it.

This is where I am at

this is my .service file

[Unit]
Description=Test Service
Wants=network-online.target influxdb.service
After=network-online.target influxdb.service

[Service]
User=testservice
Group=mainapp
SyslogIdentifier=testservice
PIDFile=/var/run/testservice.pid

Restart=on-failure
TimeoutStopSec=5
RestartSec=10

ExecStart=/usr/bin/dotnet /mainapp/app/TestSystemdService.dll

[Install]
WantedBy=multi-user.target

2 Answers2

15

If all you want is to know whether the service is running, you can query systemd:

systemctl is-active TestApp.service

You can also check whether it is inactive specifically due to a known failure:

systemctl is-failed TestApp.service

According to docs, this can be incorporated into monit as check program … with path:

check program TestApp with path "systemctl --quiet is-active TestApp"
    if status != 0 then ...

Note that systemd's PIDFile= is for telling init where to read the PID from. If the daemon itself doesn't create a pidfile, systemd certainly won't bother. If you really need one, you could have an ExecStartPost=/bin/sh -c "echo $MAINPID > /run/testapp.pid" or something similar.

grawity
  • 501,077
7

Monit have an option for matching processes using regex as well.

So , first you would do from shell: (for testing)

monit procmatch 'program'

It will match process with highest uptime. Then you can add in monit configuration :

check process myprogram matching 'progr.*'
     start program = /bin/app
     stop program = something..
fugitive
  • 183