![Monit:如何检查特定用户执行的程序?](https://linux22.com/image/642189/Monit%EF%BC%9A%E5%A6%82%E4%BD%95%E6%A3%80%E6%9F%A5%E7%89%B9%E5%AE%9A%E7%94%A8%E6%88%B7%E6%89%A7%E8%A1%8C%E7%9A%84%E7%A8%8B%E5%BA%8F%EF%BC%9F.png)
我一直在使用monit
/mmonit
来监控我的系统。它的一个特点是语法check program
,它运行一个程序并验证其返回值:
# Asserts that there are more than 40 users in the DEV DB.
check program more_than_40_users_in_dev_db
with path /home/ubuntu/servers-scripts/monitoring/more_than_40_users_in_dev_db.py
with timeout 5 seconds
every 2 cycles
if status != 0 then alert
问题是该脚本应该以用户身份运行ubuntu
,但 monit 却以用户身份运行root
。我尝试过as uid ubuntu and gid ubuntu
语法,但它似乎不适用于该check program
指令。
有没有办法以特定用户身份运行该脚本?
答案1
这可能在过去几年中发生了变化,但是现在as uid abc and as gid abc
只要 monit 以 root 身份运行(例如 systemd 服务),您就可以使用以下语法执行此操作:
# You can also shorten to 'as gid user and uid user'; both formats work for me with v5.26.0
check program somescript with path /home/user/somescript.sh as gid user and as uid user
if status != 0 then alert
其中 somescript.sh
#!/bin/bash
whoami > /home/user/whoami.out
当然,您也可以使用gid
用户所属的不同的(比如说sharing
),并且将创建具有所有权user:sharing
并包含“用户”的文件。