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?

  • What are these scripts doing and why do you want multiple instances? Are they being run with different arguments/environment or what?
    – psusi
    Jan 31, 2012 at 18:57
  • 1
    Would a simple bash script work? Something like... count the number of script processes running, if less than 100, start up the number that's missing?
    – gabe.
    Jan 31, 2012 at 18:58
  • 3
    What are you trying to accomplish? Why are some of the 300 processes dying? There's probably a better way to accomplish your task, not the least of which is if 299 instances of a script running is less good than 300, something is flawed with your application architecture. Restarting processes that died without understanding why they died will often just result in another death and incur substantial overhead as a result.
    – msw
    Feb 1, 2012 at 10:51
  • I'm working with a legacy system here that is doing processing of URLs and each needs its own thread (being written in PHP). Yes, the architecture is flawed, but still have to work with it... Working on a new one at the moment :)
    – jong
    Feb 3, 2012 at 16:23

3 Answers 3


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.


I would use pgrep|wc -l or something like that in a simple shell script. Wait one second (or less on Linux if you like to) between each check with sleep.


If your script dies and would just drop back to the shell, you can use a wrapper script for each instance:

while [ 1 == 1 ] ; do /path/to/script ; done

or you write some wrapper that forks the scripts, and uses wait/waitpid to catch dead processes.

  • 1
    I'd at least throw a sleep in there at the end just in case the script has some terminal-always-fails condition (slice is full, can't read config file, etc).. Otherwise you could have 100 processes going ape for a slice of CPU. Additionally - 100 * ( bash process + initial startup footprint of failing app) == potentially a nontrivial amount of ram) Feb 6, 2012 at 5:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .