blob: 78f9f46a26a9e6556194802702f79e1dc2b45bd3 (
plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#!/bin/bash
declare nb_proc
declare nb_runs=1
declare cmdline
declare flag_help=false
## Func defs
print_help () {
cat << EOF
Run the command in pararrel ensuring the number of sucessful exits.
Usage: $1 <OPTIONS> <CMDLINE>
Options:
-h print this message and exit gracefully
-p number of processes to spawn (required)
-n number of successful run to count (default: 1)
EOF
}
parse_params () {
local name
local delta
while getopts "hp:n:" name
do
case "$name" in
h) flag_help=true ;;
p) let "nb_proc=$OPTARG" ;;
n) let "nb_runs=$OPTARG" ;;
'?') exit 2;;
esac
done
let "delta = OPTIND - 1"
shift $delta
cmdline="$@"
}
spwan_one () {
$cmdline &
}
main () {
local procs
local ec
local good_runs=0
local children
# spwan initial processes
for (( procs = 0; procs < nb_proc; procs += 1 ))
do
spwan_one
done
while true
do
wait -n
ec=$?
echo $ec
if [ $ec -eq 0 ]; then
let "good_runs += 1"
if [ $good_runs -ge $nb_runs ]; then
break
else
spwan_one
fi
elif [ $ec -ne 3 ]; then
# error occurred or no more child left. do not continue
break
fi
done
children="$(jobs -p)"
[ ! -z "$children" ] && kill -TERM $children 2> /dev/null > /dev/null
while wait -n; do : ; done
return $ec
}
## Init script
parse_params $@
## Parametre check
if $flag_help; then
print_help
exit 0
fi
if [ -z "$nb_proc" ]; then
cat << EOF >&2
-p option not set. Run with -h option for help.
EOF
exit 2
fi
if [ -z "$cmdline" ]; then
cat << EOF >&2
CMDLINE not set. Run with -h option for help.
EOF
fi
## Main start
main
|