I have one process of which I need to run multiple instances. I would like to monitor this with Monit.
Currently this is how I'm doing it.
check program maintain_workers with path maintain_workers.sh user "command p1" 5
every "* * * * *"
if status != 0 then alert
Calling this script:
# Maintain the number of programs at certain limit
USER=$1
COMMAND=$2
LIMIT_WORKERS=$3
NUM_WORKERS=`pgrep -u "$USER" -f "$COMMAND" | wc -l`
if [ $NUM_WORKERS -lt $LIMIT_WORKERS ]
then
STARTNUM=$(( $LIMIT_WORKERS - $NUM_WORKERS ))
echo "Only $NUM_WORKERS workers detected. Starting $STARTNUM workers"
for (( i=0; i < STARTNUM ; i++ ))
do
$COMMAND &
echo "Ran worker $(( i + 1 ))"
done
fi
# Do a final check
NUM_WORKERS=`pgrep -u "$USER" -f "$COMMAND" | wc -l`
exit $(( $LIMIT_WORKERS - $NUM_WORKERS ))
But it doesn't allow me to monitor cpu or memory usage for each instance.
The other way would be to just have n
of these:
check process command0 with pidfile command0.pid
start = "command.sh start 0"
stop = "command.sh stop 0"
But that obviously is more of a pain to scale, and I figure there should be a better way. Can anyone help me out? Thank!