Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkerbot/eb2fe73e4a32f3c470d5 to your computer and use it in GitHub Desktop.
Save thinkerbot/eb2fe73e4a32f3c470d5 to your computer and use it in GitHub Desktop.

Notes

Starting/stopping processes occurs in serial, ergo make sure the start/stop commands go quickly or the whole thing hangs. I It's a good idea to use env.sh scripts to setup the execution environment so you have something fully reproducible. Here is the command to get the ruby environment via rvm.

rvm info environment | sed -n -e 's/: \{1,\}/=/' -e 's|.rvm/bin:.*|.rvm/bin:$PATH"|' -e 's/    \([A-Z]\)/export \1/p'

An example usage:

check process process_name with pidfile /path/to/pidfile.pid
  start program = "/usr/bin/env -i KEY=VALUE /path/to/env.sh /path/to/monit_exec start /path/to/pidfile.pid /path/to/stdin_file /path/to/stdout_file /path/to/logfile.log commmand args..."
  stop program  = "/usr/bin/env -i KEY=VALUE /path/to/env.sh /path/to/monit_exec stop /path/to/pidfile.pid /dev/null /dev/null /path/to/logfile.log"
  mode passive
#!/bin/bash
action="$1"
pid_file="$2"
stdin_file="$3"
stdout_file="$4"
stderr_file="$5"
shift 5
# ensure all files and directories exist
mkdir -p "$(dirname "$pid_file")"
printf "%s\n" "$stdin_file" "$stdout_file" "$stderr_file" |
while read file
do
mkdir -p "$(dirname "$file")"
if [ x"${file}" != x"${file%.fifo}" ] && ! [ -e "$file" ]
then mkfifo "$file"
fi
done
# ensure all stderr is logged
exec 2>>"$stderr_file"
log () {
level="$1"
message="$2"
progname="$(basename "${pid_file%.pid}")"
printf "[$(date +%Y-%m-%dT%H:%M:%S%z)] %s %s %s %s
" "$level" $$ "$progname" "$message" >&2
}
case $action in
start)
log INFO "$action - $* < $stdin_file > $stdout_file"
echo $$ > "$pid_file";
exec "$@" < "$stdin_file" > "$stdout_file"
;;
stop)
log INFO "$action"
pid="$(cat "$pid_file")"
kill -term "$pid" >&2
num_tries=60
try_num=0
while [ "$try_num" -lt "$num_tries" ] && kill -0 "$pid" > /dev/null 2>&1
do
log INFO "wait for exit $((try_num + 1))/$num_tries ($pid)"
sleep 1
try_num=$((try_num + 1))
done
if [ "$try_num" -eq "$num_tries" ]
then
log WARN "forcing exit ($pid)"
kill -kill "$pid" >&2
fi
rm "$pid_file"
;;
esac
set httpd port 2812 and use address localhost allow localhost
set logfile /tmp/monit.log
include /Users/simonchiang/.monit.d/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment