Keep a certain number of background processes running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keep a certain number of background processes running
# 8  
Old 03-24-2009
MySQL One good script to keep a certain number of process running

I found this script in Go deh!: Batch Process Runner in bash shell
Code:
  1 #!/bin/bash
  2 
  3 ##
  4 ## process_runner.sh <concurrent> <total procs>
  5 ##
  6 ## Example script given the maximum number of processes to
  7 ## run concurrently, c, and the total number of processes, n
  8 ## runs at most c, processes in the background until all n
  9 ## Have been run.
 10 ##
 11 ## Author Donald 'Paddy' McCarthy Dec. 17 2007
 12 ##
 13 
 14 # how many processes to run in parallel
 15 concurrent=$1
 16 # total processes to run
 17 maxprocs=$2
 18 
 19 printf "\n## STARTING %i Processes, %i at a time\n\n" \
 20   $maxprocs $concurrent
 21 
 22 
 23 # main loop wait time between checking background procs.
 24 tick=1
 25 
 26 
 27 # dummy processes sleep for a random time
 28 function runproc {
 29   local -i n=$1
 30   local -i j
 31   (( j = 5+$RANDOM*10/32767 ))
 32   #(date; printf "#>>>JOB %i : Sleeping for %i\n" $n $j)
 33   printf "OUT JOB ,%03i, Sleep for ,%2i,  , @,%s\n" $n $j "`date`"
 34   sleep $j
 35   returned=$?
 36   printf "IN  JOB ,%03i, Slept for ,%2i, returned ,%i, @,%s\n" \
 37     $n $j $returned "`date`"
 38   #(date; printf "#<<<JOB %i : Slept for %i\n" $n $j)
 39 }
 40 
 41 function print_runstats {
 42   printf '# %i Jobs in background. %i/%i started\n\n'  \
 43     `jobs -r|wc -l` $ran $maxprocs
 44 }
 45 
 46 # Bash array running keeps track of the background process numbers
 47 # Start with nothing running (sentinel value will not be a process number
 48 for ((i=0; i<$concurrent; i+=1 )); do running[$i]=123456789; done
 49 
 50 ran=0
 51 until
 52   while [ $ran -lt $maxprocs ]; do
 53     for ((p=0; p<$concurrent; p+=1 )); do
 54       proc=${running[$p]}
 55       # Over all running processes...
 56       # $proc still running?
 57       ps -p $proc | fgrep $proc >/dev/null
 58       if [ $? -ne '0' ] ; then
 59         # Not found  i.e. finished
 60         # Run another in background and store the proc number
 61         runproc $ran &
 62         running[$p]=$!
 63         ((ran+=1))
 64         if [ $ran -ge $maxprocs ]; then break 1; fi
 65       fi
 66     done
 67     sleep $tick
 68     # Status
 69     print_runstats
 70   done
 71 
 72   sleep $tick
 73   # Status
 74   print_runstats
 75 do [  `jobs -r|wc -l` -eq 0 ]
 76 done
 77 wait
 78 printf "\n## FINISHED\n"
 79 
 80 exit 0
 81 
 82 sample_output=<<!
 83 bash$ ./process_runner.sh 2 5
 84 
 85 ## STARTING 5 Processes, 2 at a time
 86 
 87 OUT JOB ,000, Sleep for ,10,  , @,Tue Dec 18 09:26:00 GMTST 2007
 88 OUT JOB ,001, Sleep for , 8,  , @,Tue Dec 18 09:26:00 GMTST 2007
 89 # 2 Jobs in background. 2/5 started
 90 
 91 # 2 Jobs in background. 2/5 started
 92 
 93 # 2 Jobs in background. 2/5 started
 94 
 95 # 2 Jobs in background. 2/5 started
 96 
 97 # 2 Jobs in background. 2/5 started
 98 
 99 # 2 Jobs in background. 2/5 started
100 
101 IN  JOB ,001, Slept for , 8, returned ,0, @,Tue Dec 18 09:26:09 GMTST 2007
102 # 1 Jobs in background. 2/5 started
103 
104 OUT JOB ,002, Sleep for ,11,  , @,Tue Dec 18 09:26:09 GMTST 2007
105 IN  JOB ,000, Slept for ,10, returned ,0, @,Tue Dec 18 09:26:10 GMTST 2007
106 # 2 Jobs in background. 3/5 started
107 
108 OUT JOB ,003, Sleep for , 7,  , @,Tue Dec 18 09:26:11 GMTST 2007
109 # 2 Jobs in background. 4/5 started
110 
111 # 2 Jobs in background. 4/5 started
112 
113 # 2 Jobs in background. 4/5 started
114 
115 # 2 Jobs in background. 4/5 started
116 
117 # 2 Jobs in background. 4/5 started
118 
119 IN  JOB ,003, Slept for , 7, returned ,0, @,Tue Dec 18 09:26:18 GMTST 2007
120 # 1 Jobs in background. 4/5 started
121 
122 OUT JOB ,004, Sleep for , 6,  , @,Tue Dec 18 09:26:19 GMTST 2007
123 # 2 Jobs in background. 5/5 started
124 
125 IN  JOB ,002, Slept for ,11, returned ,0, @,Tue Dec 18 09:26:20 GMTST 2007
126 # 1 Jobs in background. 5/5 started
127 
128 IN  JOB ,004, Slept for , 6, returned ,0, @,Tue Dec 18 09:26:25 GMTST 2007
129 
130 ## FINISHED
131 !
132 
133 
134

All credits go to the devoloper.
I am using it.
it is working flawlessly.

Last edited by vbe; 03-24-2009 at 12:09 PM.. Reason: format and tags added...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I find the number of processes running on root?

Is there a certain man command I'm missing here? I searched in ps but I couldn't find something that would give me the number of processes running on root. I only want to see the number of processes, not the processes itself. (2 Replies)
Discussion started by: l3monz
2 Replies

2. Shell Programming and Scripting

Need help on background processes

Hi, I have a schell script parent.ksh from which I am calling three background processes a.ksh,b.ksh and c.ksh. Once these three processes completes the next step in parent.ksh should execute. How to achieve this? Please help me.... Thanks... (1 Reply)
Discussion started by: ravinunna
1 Replies

3. UNIX for Dummies Questions & Answers

How do you print the number of processes that each user is currently running in Unix?

Ok, so I know there's a way to do this, but I've been trying to find out all afternoon with no luck. I think it should print out something like this: 1 bin 2 daemon 6 duo Where the numbers on the left are the number of processes being run by the user whose name is listed on the right. Is... (4 Replies)
Discussion started by: Duo11
4 Replies

4. UNIX for Dummies Questions & Answers

Query on Running Multiple processes in background

HI All , Pardon me for asking some very basic questions, I would be grateful if someone can help. I am trying to execute a shell script which runs multiple processes in background. It includes various operations like copying , DB operations etc etc. Now problem is that the complete script... (6 Replies)
Discussion started by: gpta_varun
6 Replies

5. Shell Programming and Scripting

How to Control Number of Processes Running

Hi Is there a way to count how many processes a script has started, count how many of these have finished, and make the script wait if their difference goes over a given threshold? I am using a script to repeatedly execute a code (~100x) which converts 2 data files into one .plt which is in... (4 Replies)
Discussion started by: drbones
4 Replies

6. Shell Programming and Scripting

Background Processes

Ok guys so I have my first dummy shell almost done except for one tiny part: I do not know how to run a process in the background, from the code! I already know how to do that in a normal shell: $ program & However, no clue when it comes to how to program that thing. :eek: A very... (2 Replies)
Discussion started by: Across
2 Replies

7. Shell Programming and Scripting

Waiting for an arbitrary background process (limiting number of jobs running)

Hi, I'm trying to write a script to decompress a directory full of files. The decompression commands can run in the background, so that many can run at once. But I want to limit the number running at any one time, so that I don't overload the machine. Something like this: n=0 for i in *.gz... (15 Replies)
Discussion started by: p.f.moore
15 Replies

8. Solaris

About running processes in background

Hi, I need to establish a procedure that will start an application in background each time my remote Solaris server is (re)started. This would be a kind of daemon. I am no sysadmin expert, so I am looking for pointers. How should I proceed? What are the main steps? Thanks, JVerstry (9 Replies)
Discussion started by: JVerstry
9 Replies

9. Shell Programming and Scripting

Determine Number of Processes Running

I am pretty new to unix, and I have a project to do. Part of the project asks me to determine the number of processes running and assign it to a variable. I know how to every part of the project but determine the number of processes running. How can I get just the number of processes... (4 Replies)
Discussion started by: wayne1411
4 Replies

10. Shell Programming and Scripting

Running two processes in background

hi there, here's what i need in my korn-shell: ... begin korn-shell script ... nohup process_A.ksh ; nohup process_B.ksh & ... "other stuff" ... end lorn-shell script in plain english i want process A and process B to run in the background so that the script can continue doing... (6 Replies)
Discussion started by: jacob_gs
6 Replies
Login or Register to Ask a Question