-
Notifications
You must be signed in to change notification settings - Fork 33
/
generalParallel
executable file
·74 lines (61 loc) · 1.54 KB
/
generalParallel
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
#########################################################################
# File Name: parallel.sh
# Author: C.J. Liu
# Mail: samliu@hust.edu.cn
# Created Time: Tue 06 Dec 2016 04:39:49 PM CST
#########################################################################
#!/usr/bin/env bash
params=$#
scripts=$1
# Set default threads as 20
threads=${2:-20}
echo "Notice: Your did parallel in $threads threads."
echo ""
function usage {
Description="Notice: The script aimed at running shell command in general parallel."
Usage="Uage: generalParallel shell_scripts.sh parallel_number(default:20)"
ErrorNo="Error: Number of arguments must be 1 or 2"
ErrorScript="Error: suffix of script file must be sh."
if [ "$params" -lt 1 -o "$params" -gt 2 ]; then
echo $ErrorNo
echo $Description
echo $Usage
exit 1
fi
if [ "${scripts#*.}" != "sh" ]; then
echo $ErrorScript
echo $Description
echo $Usage
exit 1
fi
if [ $threads -gt 20 ]; then
echo "Warnings: Your THREADS is $threads"
echo "Warnings: Be careful! Your THREADS EXCEED 20!!!"
fi
}
# Usage
usage
# Make fifo file
pipe="${HOME}/tmp/$$.fifo"
mkfifo $pipe
exec 6<>$pipe
rm -rf $pipe
[[ $threads -gt 20 ]] && echo "Warnings: Your THREADS is $threads" && echo "Warnings: Be careful! Your THREADS EXCEED 20!!!"
for (( i=0; i<$threads; i++ ))
do
echo ""
done >&6
cat $scripts | while read cmd
do
read -u6
{
cmd=${cmd%%&}
echo "Notice - Your are running $cmd on the $HOSTNAME"
eval $cmd
echo "Success - $cmd was done!!! on the $HOSTNAME"
echo "" >&6
}&
done
wait
exec 6>&-
wait