Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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 '12 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 '12 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 '12 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 '12 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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
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) –  synthesizerpatel Feb 6 '12 at 5:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.