我一直在使用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
并包含“用户”的文件。