-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforking.sh
59 lines (52 loc) · 2.13 KB
/
forking.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
#!/usr/bin/env bash
#-------------------------------------------------------------------------------
# includable functions to fork stuff to the background and wait for them
# Functions:
# - b "<command>" : Background
# - wbit_ : wait for all commands we forked off
# - reset_b : reset the variable tracking processes in the background
#-------------------------------------------------------------------------------
#=== FUNCTION ================================================================
# NAME: b
# DESCRIPTION: forks commands to background
# PARAMETERS: commands fo be forked
#===============================================================================
b() {
# shellcheck disable=2048
${*} &
b_WatchPIDs="${b_WatchPIDs} $!" # add child to list of PIDs to watch
} # end of function b
#=== FUNCTION ================================================================
# NAME: wait_b
# DESCRIPTION: waits for the precesses we forked off to finish
# PARAMETERS: none
#===============================================================================
wait_b() {
# shellcheck disable=2086
wait ${b_WatchPIDs} 2>/dev/null # wait till all children are done
reset_b # reset the variable in case we need to use it later again
} # end of function wait_b
#=== FUNCTION ================================================================
# NAME: reset_b
# DESCRIPTION: resets the variable tracking processes in the background
# PARAMETERS: none
#===============================================================================
reset_b() {
b_WatchPIDs="" # empty variable
} # end of function reset_b
reset_b
#-------------------------------------------------------------------------------
# example code
#-------------------------------------------------------------------------------
#
# for count in $(seq 1 15)
# do
# time="$(bc <<< "0.$RANDOM * 15")"
# b sleep $time
# echo "+ forked off a sleep waiting $time seconds"
# done
#
# echo "waiting for all background processes to finish"
# wait_b
# echo "done"
#-------------------------------------------------------------------------------