0

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!

Oxidator
  • 126
  • 4
  • Looks like a poorly written daemon. My opinion - you should lay down some napalm on it, then burn it to the ground. Then rewrite from scratch. It's a daemon master process task - to monitor the number of children and to fork them when needed. – drookie Jan 13 '16 at 07:56
  • @drookie Though I don't disagree with you, it's a library I use and I'd rather not – Oxidator Jan 13 '16 at 08:11
  • The thing is, there's no easy way out from your situation. And people using a bunch of workers always end up with similar questions. So... you probably are the most competent one right now. – drookie Jan 13 '16 at 08:27

0 Answers0