Maintain 100 instances of a program running

monitoringprocess

I'm currently using supervisord to maintain 100 instances of a script running at once. If any die then it starts up a new one.

However it seems to be struggling maintaining larger numbers (>300 processes) and I'm looking for a replacement. Monit doesn't seem to do what I want as it monitors individual scripts and does not seem to be able to easily watch 100 instances of the same script.

Any suggestions on a different tool I could use?

Best Answer

You can use:

ps h --ppid $$ | wc -l

to get the number of child processes from a bash script (remember this includes ps). So if you want to have 1000 processes you check to see if that returns 1001. If not fire them up with:

cmd &

so that they run as children of the current script (and therefore get included in the count.) You can then sleep for a bit, then check again in a loop forever. One thing to keep in mind is if you are spawning other processes you will need to modify the ps command to filter the processes you want.

That first command is core piece of the puzzle, it should just be a little more until you have your script.

Related Question