Bash: Regulating the number of processes a script can spawn


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash: Regulating the number of processes a script can spawn
# 1  
Old 05-05-2010
Bash: Regulating the number of processes a script can spawn

I've been working on some scripts in which I spawn some background processes. I'd like to be able to limit the number of processes, but have my script spawn additional processes as previous tasks finish. So, let's say I have 20 tasks to complete. Any given task could take from 1 to 10 minutes. I want to have only 3 of these background tasks going at a time, but when one completes I want another to start up until all 20 are finished. I've come up with what seems like a sloppy but effective way to do this involving "while" and "jobs", but I'd appreciate any suggestions on good ways to accomplish this general type of scripting task efficiently. How do you do this?
# 2  
Old 05-05-2010
have them each establish any one of several specifically named lock-files...and then govern the available slots.
# 3  
Old 05-06-2010
You could also do this quite easily with a makefile. Make has built-in mechanisms for handling multiple jobs. Here's a generic makefile that feeds ".f" files into your programs' standard input, and puts its standard output into ".g" files:
Code:
# File should be named Makefile
# Anywhere you see eight leading spaces, its actually a tab

# Tell it .f and .g are suffixes to convert
.SUFFIXES:
.SUFFIXES: .f .g

# Rule telling it how to create *.g from *.f
.f.g :
        ./myscript.sh < "$<" > "$@"

With Makefile in your current directory, when you run
Code:
make file.g

it will feed file.f into ./myscript.sh 's standard input, and create file.g from its standard output.

You can specify more than one target:
Code:
make 1.g 2.g 3.g 4.g 5.g 6.g 7.g 8.g 9.g

And you can tell it how many processes it should allow to run at once:
Code:
# Allow two simultaneous processes
make -j 2 1.g 2.g 3.g 4.g 5.g 6.g 7.g 8.g 9.g

# 4  
Old 05-07-2010
Thanks for the replies. I've worked with lock files a little before, but never for process regulation. Good idea. The makefile option is something I've never tried, but it's really interesting. Looks like I have something new for the "to be studied" list. Again, many thanks for the suggestions.
# 5  
Old 05-07-2010
Makefiles are more or less designed for process management. Its original, and still primary function is compiling source code but it can be applied to lots of problems as long as your program can be though of as converting one kind of data into another, repeatably(always the same input means always the same output.) Given more complicated rule relationships it can deduce the proper order to build things in, i.e. tell it how to turn .a into .b, .b into .c, and .c into .d, run 'make file.d', it will find file.a and go through all the steps to produce file.d from it.
# 6  
Old 05-08-2010
# 7  
Old 05-08-2010
This will run 100 jobs with 10 of them running at any one time:

Code:
#!/bin/bash

set -bm

startjob() {
  if [ $count -lt $total_jobs ]; then
    yourjob.sh &
    count=$(($count+1))
  fi
}

max_parallel=10
total_jobs=100

trap 'startjob' SIGCHLD

count=0
started=0
while [ $started -lt $max_parallel ]; do
  startjob
  started=$(($started+1))
done

wait

You just have to be really careful to be sure the only child processes your script spawns after the trap command are your jobs. No "ls" commands, no "rm", no "expr", nothing. Only shell built-ins.

Last edited by achenle; 05-08-2010 at 01:40 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

(bash) Script Processes in Parallel

Hello all, I tried to parralise my treatments but after a while 'ps -ef' display all child process <defunct> (zombie) Parent bash script to process all files (>100000) in directory: for filename in /Data/*.txt; do ./child_pprocess.sh $filename & done exit(0)I understand that the... (1 Reply)
Discussion started by: namnetes
1 Replies

2. Shell Programming and Scripting

Is it possible to Divide a negative number in bash script

I am using a small script to divide some numbers in a given file and display the output in another file. I am getting the following error basename: invalid option -- '5' Try `basename --help' for more information. (standard_in) 1: syntax error The script is : #!/bin/bash for i in `cat... (4 Replies)
Discussion started by: kmnr877
4 Replies

3. Shell Programming and Scripting

SPAWN Multiple Processes in Unix

Hi, I have three files in my IN directory.Each file should be copied 25 times using for loop.Each file processing should run in parallel?How to spawn multiple processes in unix?Any help would be appreciated. Thanks, Liyakath (7 Replies)
Discussion started by: liyakathali
7 Replies

4. Shell Programming and Scripting

script for multi-threaded bash processes

hey everyone, I'm having some trouble breaking down some code. It's simple a control script that takes machines meant to be backed up from a list. Then according to that will run multi-threaded processes up until the specified thread limit. for example if there are 4 machines to be backed up,... (2 Replies)
Discussion started by: terrell
2 Replies

5. Shell Programming and Scripting

need to kill a number of processes with name "XYZ" at a time using shell script

Hi, when i grep for the process "XYZ" , there will be some good number of processes with that name, i want to kill all the these processes at a time using shell script? Any help needed for this action. Thanks Regards, Anil (6 Replies)
Discussion started by: anilmanepu
6 Replies

6. Shell Programming and Scripting

Printing the line number in bash script

Hi, I would like to know how do I print the line # in a script. My requirement is, I have a script which is about ~5000 lines long. If there are any errors happen I just exit. And I would like to add the line # of the script where the error happened. Thanks, (6 Replies)
Discussion started by: suryaemlinux
6 Replies

7. Shell Programming and Scripting

Defining Dynamic Number of Variables in a Bash Script

Code: $ cat test.bash #!/bin/bash job=$1 steps=$2 num=$(echo "$@" | wc -w) Example Submission: $ ./test.bash BS01 3 1 2 3 What: (2 Replies)
Discussion started by: mkastin
2 Replies

8. Programming

Program to spawn multiple processes

I'm trying to make a program that will spawn multiple child processes then exit. I'm having trouble figuring out how to do this since after I fork, the child process begins running the program again (never ending). int main(void){ for(int i = 0; i < 3; i++){ fork(); }... (1 Reply)
Discussion started by: cagney58
1 Replies

9. Shell Programming and Scripting

shell script for getting pid of spawn processes from shell

Hi, I am new this forum. I request you peoples help in understanding and finding some solution to my problem. Here it goes: I need to perform this set of actions by writing a shell script. I need to read a config file for the bunch of processes to execute. I need to fecth the pid of... (4 Replies)
Discussion started by: sachin4sachi
4 Replies

10. Programming

Howto spawn multiple child processes and wait?

As far as I can tell, the bash wait command waits for a logical "AND" of all the child processes. Assuming I am coding in C: (1) What is the function I would use to create multiple bash child process running perl? (2) What is the function I would use to reinvent the bash wait command so I... (4 Replies)
Discussion started by: siegfried
4 Replies
Login or Register to Ask a Question