How can I use Monit to monitor a service that does not have a /var/run/ pid file or how can I create a pid file?

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.


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..

Tags:

Pid

Monit

Systemd