Using pipe to concurrently execute programs in bash

http://www.cnblogs.com/qinqiao/p/concurrent_by_shell_mkfifo.html

 1 #!/bin/bash
 2 
 3 your_func()
 4 {   # use your cmd or func instead of sleep here. don't end with background(&)
 5     date +%s
 6     echo "scp HOSTNAME:/home/USER/path/from/file.$1 REMOTE_HOST:/home/USER/path/to/"
 7     sleep 2
 8 }
 9 
10 concurrent()
11 {   # from $1 to $2, (included $1,$2 itself), con-current $3 cmd
12     start=$1 && end=$2 && cur_num=$3
13 
14     # ff_file which is opened by fd 4 will be really removed after script stopped
15     mkfifo   ./fifo.$$ &&  exec 4<> ./fifo.$$ && rm -f ./fifo.$$
16 
17     # initial fifo: write $cur_num line to $ff_file
18     for ((i=$start; i<$cur_num+$start; i++)); do
19         echo "init time add $i" >&4
20     done
21 
22     for((i=$start; i<=$end; i++)); do
23         read -u 4   # read from mkfifo file
24         {   # REPLY is var for read
25             echo -e "-- current loop: [cmd id: $i ; fifo id: $REPLY ]"
26 
27             your_func $i
28             echo "real time add $(($i+$cur_num))"  1>&4 # write to $ff_file
29         } & # & to backgroud each process in {}
30     done
31     wait    # wait all con-current cmd in { } been running over
32 }
33 
34 concurrent 0 8 3
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s