3 # process manager helper methods for bash.
5 # relies on the built-in process management to run a bunch of processes
6 # in the background, but will limit total number running to a maximum.
7 # demonstration method at the end of the file shows how to use the
8 # process managing methods.
12 #hmmm: revisions desired someday:
13 # + allow number of max processes to be passed in.
16 # number of background processes.
19 # maximum number of simultaneous background processes.
22 # number of processes to wait for if we hit the maximum.
25 function start_background_action()
27 # launch the commands provided as parms in a subshell.
28 (for i in "${@}"; do eval "$i" ; done)&
29 #echo bg_count pre inc is $bg_count
31 #echo bg_count post inc is $bg_count
34 function take_inventory()
36 start_background_action \
37 'echo "taking inventory..."' \
38 'bash $FEISTY_MEOW_SCRIPTS/core/inventory.sh'
43 start_background_action \
44 'echo "your nechung oracle pronouncement of the moment..."' \
48 function login_on_xcg()
50 start_background_action \
51 'echo "summing directory output coming up..."' \
52 'perl $FEISTY_MEOW_SCRIPTS/files/summing_dir.pl'
55 # takes the number of processes to wait for, or just waits for one of them.
56 function wait_on_backgrounders()
58 local wait_count="$1"; shift
60 target_count=$(($bg_count - $wait_count))
61 if (($target_count < 1)); then target_count=1; fi
62 echo before waiting, count is $bg_count
63 while (($bg_count > $target_count - 1)); do
64 # wait for one job, let bash pick which.
66 echo bg_count pre dec is $bg_count
68 echo bg_count post dec is $bg_count
70 echo "done waiting, background process count is down to $bg_count."
73 # happily launches off different actions as background processes.
74 launcher_demonstrator()
78 which=$(($RANDOM % 3))
79 #hmmm: not asynch yet! make it so!
86 # we have reached the limit on processes and need to wait for a few, defined by
87 # procs_to_await variable at top.
88 if (($bg_count > $max_bg_procs - 1)); then
89 echo "have reached $max_bg_procs background processes threshold; waiting for $procs_to_await of them to complete."
90 wait_on_backgrounders $procs_to_await
96 launcher_demonstrator;