-
Notifications
You must be signed in to change notification settings - Fork 1
/
parallel.sh
78 lines (61 loc) · 1.66 KB
/
parallel.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
#
# This script executes $maxjobs commands in parallel
# It will monitor for job completion and automatically
# start the next job so there are always $maxjobs running
#
#
# For example, if you want to run some program with a
# number of different combinations of parameters (param1 and param2 in
# the example below), you can place it in the next_job function and
# pass the parameter combination required for that job
#
#
#max # of jobs to run in parallel
let maxjobs=6
#how often to check if a job has completed (in seconds)
let pollTime=15
next_job() {
#place the code you want to execute in parallel in this function
#any code in here will be executed in the background along
#with other jobs. pass any parameters required
p1=$1
p2=$2
echo "executing job with param $p1 $p2"
#execute some program here
}
#number of jobs currently running
let running=0
#example with two parameters
for param1 in 1 2 3 4
do
for param2 in 9 8 7 6
do
#start a new job
next_job $param1 $param2 &
let running=$running+1
#maximum number of jobs are running
if [ $running -eq $maxjobs ]
then
#wait for any of the jobs to finish
while [ 1 ] ;
do
pids=`jobs -p`
set -- $pids
#max jobs still running
if [ $# -eq $maxjobs ];
then
sleep $pollTime
else
#at least one job finished
#break and let another one start
let running=$running-1
break
fi
done
fi
done
done
#wait for any remaining jobs
wait
#do any post processing