5 # Name : process manager helper methods
6 # Author : Chris Koeritz
7 # Rights : Copyright (C) 2015-$now by Author
11 # Relies on the built-in process management in bash to run a bunch of
12 # processes in the background, but will limit total number running to a
13 # maximum count. There is a demonstration method at the end of the file
14 # that shows how to use the process management functions.
17 # This script is free software; you can modify/redistribute it under the terms
18 # of the GNU General Public License. [ http://www.gnu.org/licenses/gpl.html ]
19 # Feel free to send updates to: [ fred@gruntose.com ]
22 #hmmm: revisions desired someday:
23 # + allow number of max processes to be passed in.
26 # number of background processes.
29 # maximum number of simultaneous background processes.
32 # number of processes to wait for if we hit the maximum.
35 function start_background_action()
37 # launch the commands provided as parms in a subshell.
38 (for i in "${@}"; do eval "$i" ; done)&
39 #echo bg_count pre inc is $bg_count
41 #echo bg_count post inc is $bg_count
44 function take_inventory()
46 start_background_action \
47 'echo "taking inventory..."' \
48 'bash $FEISTY_MEOW_SCRIPTS/core/inventory.sh'
53 start_background_action \
54 'echo "your nechung oracle pronouncement of the moment..."' \
55 '$FEISTY_MEOW_BINARIES/nechung'
60 start_background_action \
61 'echo "summing directory output coming up..."' \
62 'perl $FEISTY_MEOW_SCRIPTS/files/summing_dir.pl'
65 # takes the number of processes to wait for, or just waits for one of them.
66 function wait_on_backgrounders()
68 local wait_count="$1"; shift
70 target_count=$(($bg_count - $wait_count))
71 if (($target_count < 1)); then target_count=1; fi
72 echo before waiting, count is $bg_count
73 while (($bg_count > $target_count - 1)); do
74 # wait for one job, let bash pick which.
76 echo bg_count pre dec is $bg_count
78 echo bg_count post dec is $bg_count
80 echo "done waiting, background process count is down to $bg_count."
83 #hmmm: the demo app here raises some concerns--how do we know the bg count is right?
84 # what if something died and we didn't check it?
85 # will the thing that looks at bg count adjust it if there are actually no waiting processes?
87 # happily launches off different actions as background processes.
88 launcher_demonstrator()
90 # run a limited number of loops, since we don't want to do this forever.
92 while ((loops-- > 0)); do
94 which=$(($RANDOM % 3))
95 #hmmm: not asynch yet! make it so!
102 # we have reached the limit on processes and need to wait for a few, defined by
103 # procs_to_await variable at top.
104 if (($bg_count > $max_bg_procs - 1)); then
105 echo "have reached $max_bg_procs background processes threshold; waiting for $procs_to_await of them to complete."
106 wait_on_backgrounders $procs_to_await
112 launcher_demonstrator;