如何使用 Monit 监视没有 /var/run/ pid 文件的服务或如何创建 pid 文件?

The*_*l26 4 pid systemd monit

我正在使用 Debian 9.8

我有一个运行我的 dotnet 程序的服务。我想用 monit 监视它,但在所有示例中,您都需要在 /var/run 中引用 .pid 文件,但我的 dotnet 程序在 /var/run 中没有 .pid 文件。

所以我将 PIDFile=/var/run/testservice.pid 添加到我的服务的 .service 文件中,但是当我启动它时它没有创建文件。

这就是我所在的地方

这是我的 .service 文件

[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
Run Code Online (Sandbox Code Playgroud)

use*_*686 8

如果您只想知道服务是否正在运行,您可以查询 systemd:

systemctl is-active TestApp.service
Run Code Online (Sandbox Code Playgroud)

您还可以检查它是否因已知故障而处于非活动状态:

systemctl is-failed TestApp.service
Run Code Online (Sandbox Code Playgroud)

根据文档,这可以合并到 monit 中check program … with path

check program TestApp with path "systemctl --quiet is-active TestApp"
    if status != 0 then ...
Run Code Online (Sandbox Code Playgroud)

请注意,systemdPIDFile=用于告诉 init 从何处读取PID。如果守护进程本身不创建 pidfile,systemd 肯定不会打扰。如果你真的需要一个,你可以有一个ExecStartPost=/bin/sh -c "echo $MAINPID > /run/testapp.pid"或类似的东西。


小智 5

Monit 也有使用正则表达式匹配进程的选项。

所以,首先你会从 shell 做:(用于测试)

monit procmatch 'program'

它将匹配具有最高正常运行时间的进程。然后你可以添加 monit 配置:

check process myprogram matching 'progr.*'
     start program = /bin/app
     stop program = something..
Run Code Online (Sandbox Code Playgroud)