大网数据广告活动
服务器运维问答

How to make Monit “check process” conditional inside docker based on docker run args?

· 52CE 我爱测

I would like to make Monit's "check process" work conditionally based on an env variable. Here's the pseudo-code of what I would like to achieve(not a valid monit config syntax): [ -n "$run_foo" ] && check process foo .... My use…

问题原文

原帖链接 · Dmitry z · CC BY-SA 3.0

I would like to make Monit's "check process" work conditionally based on an env variable.

Here's the pseudo-code of what I would like to achieve(not a valid monit config syntax):

[ -n "$run_foo" ] && check process foo ....

My use case: I have a docker image that starts monit process like this:

CMD ["/bin/monit", "-c","/config/monit.conf"]

Monit then starts various daemons.

Now, I would like to be able to tell at run time which daemons I want to start in a particular container created from this image, like this:

docker run --env="run_foo=1;run_bar=1" ...

I am aware of monit unmonitor, and similar args, however I believe they don't serve my purpose.

Thought about something like this:

start program="/bin/bash -c '[ -n \"$run_foo\" ] && /bin/foo .."

However, seems like it will cause monit to spin continuously trying to start and monitor things that shouldn't be started.

I am aware of possibility of using different monit config files, or using different docker images - these don't qualify as an answer.

Other ideas appreciated.

已采纳解答

原帖链接 · boppy · CC BY-SA 4.0

Sadly that is not how Monit is working.

Monit is there to react to states you define. If you use it as init system (what is totally fine and working) you have to have a config file that is built for your needs.

You could, however, try to hack it with a little sed or something.

The idea is to have a monitrc with all configs, but all inactive and activate them on first start. Like:

#check process foo with pidfile /run/foo.pid # proc-foo
#  onreboot nostart # proc-foo
#  mode passive # proc-foo

#check process bar with pidfile /run/bar.pid # proc-bar
#  onreboot nostart # proc-bar
#  mode passive # proc-bar

check program firststart path "/bin/bash -c /tmp/firststart.sh" # first-start

While /tmp/firststart.sh is something like:

#!/usr/bin/env bash

# Uncomment Lines Per Config:
[[ -n "${run_foo}" ]] && sed -i 's/^#\(.*\) # proc-foo$/\1/' /etc/monitrc
[[ -n "${run_bar}" ]] && sed -i 's/^#\(.*\) # proc-bar$/\1/' /etc/monitrc

# Delete FirstStart Line
sed -i '/^.*# first-start$/d' /etc/monitrc

# Force reload of monit config
kill -SIGHUP $(cat /run/monit.pid)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-03-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。