Run a single check and conditional actions from the host scheduler.
1. Prepare the check
Complete the Python installation. Select test containers whose names start with web, or replace the selector. Quote patterns to prevent shell expansion.
cron requires a state file and at least one --cmd-if rule or --cmd command. It runs once and exits.
2. Test without Docker actions
monit-docker --name 'web*' cron \
--state-file "$HOME/.local/state/monit-docker/web.json" \
--cooldown 300 --dry-run \
--cmd-if 'mem_percent > 90 ? restart'The rule proposes a restart when memory exceeds 90%. No decision is printed if it does not match. Dry-run reads the real Docker Engine but performs no action and does not update JSON state. It may create the directory and lock file.
3. Schedule every minute
After reviewing the result, create ~/.local/bin/monit-docker-web with this content. This version performs matching restarts.
mkdir -p "$HOME/.local/bin" "$HOME/.local/state/monit-docker"#!/bin/sh
export MONIT_DOCKER_LOGFILE="$HOME/.local/state/monit-docker/monit-docker.log"
export MONIT_DOCKER_RUNTIMEDIR="$HOME/.local/state/monit-docker/run"
exec "$HOME/.local/share/monit-docker/venv/bin/monit-docker" \
--name 'web*' cron \
--state-file "$HOME/.local/state/monit-docker/web.json" \
--cooldown 300 \
--cmd-if 'mem_percent > 90 ? restart'chmod 700 "$HOME/.local/bin/monit-docker-web"
crontab -eAdd this line, replacing USER with your account and adjusting its home directory. The same account needs Docker access.
* * * * * /home/USER/.local/bin/monit-docker-web >> /home/USER/.local/state/monit-docker/cron.log 2>&1
4. Tune timing
--cooldown 300 spaces attempts of the same rule for the same full container ID by five minutes. Failed attempts also reserve this cooldown.
To require five minutes of observed threshold breaches before acting, add --trigger-after 300 --max-gap 120. With a once-per-minute cron, an observation gap over 120 seconds resets the wait. Dry-runs do not accumulate this history.
Keep the state file and its directory on persistent local storage. The same path coordinates concurrent invocations. Do not delete the .lock file to unlock an execution.
5. Verify and stop
Inspect cron.log, the agent log and the container in Docker. A cooldown result means timing temporarily blocks another attempt. Exit codes 117 and 118 indicate a busy lock and a state error respectively.
To stop scheduling, remove the crontab entry. Keep the state file to preserve cooldowns when resuming.