5 # Name : process manager helper methods
6 # Author : Chris Koeritz
7 # Rights : Copyright (C) 2015-$now by Author
11 # Turns Unix format text files into DOS format text files.
12 # relies on the built-in process management to run a bunch of processes
13 # in the background, but will limit total number running to a maximum.
14 # demonstration method at the end of the file shows how to use the
15 # process managing methods.
18 # This script is free software; you can modify/redistribute it under the terms
19 # of the GNU General Public License. [ http://www.gnu.org/licenses/gpl.html ]
20 # Feel free to send updates to: [ fred@gruntose.com ]
23 # process manager helper methods for bash.
25 # relies on the built-in process management to run a bunch of processes
26 # in the background, but will limit total number running to a maximum.
27 # demonstration method at the end of the file shows how to use the
28 # process managing methods.
32 #hmmm: revisions desired someday:
33 # + allow number of max processes to be passed in.
36 # number of background processes.
39 # maximum number of simultaneous background processes.
42 # number of processes to wait for if we hit the maximum.
45 function start_background_action()
47 # launch the commands provided as parms in a subshell.
48 (for i in "${@}"; do eval "$i" ; done)&
49 #echo bg_count pre inc is $bg_count
51 #echo bg_count post inc is $bg_count
54 function take_inventory()
56 start_background_action \
57 'echo "taking inventory..."' \
58 'bash $FEISTY_MEOW_SCRIPTS/core/inventory.sh'
63 start_background_action \
64 'echo "your nechung oracle pronouncement of the moment..."' \
68 function login_on_xcg()
70 start_background_action \
71 'echo "summing directory output coming up..."' \
72 'perl $FEISTY_MEOW_SCRIPTS/files/summing_dir.pl'
75 # takes the number of processes to wait for, or just waits for one of them.
76 function wait_on_backgrounders()
78 local wait_count="$1"; shift
80 target_count=$(($bg_count - $wait_count))
81 if (($target_count < 1)); then target_count=1; fi
82 echo before waiting, count is $bg_count
83 while (($bg_count > $target_count - 1)); do
84 # wait for one job, let bash pick which.
86 echo bg_count pre dec is $bg_count
88 echo bg_count post dec is $bg_count
90 echo "done waiting, background process count is down to $bg_count."
93 # happily launches off different actions as background processes.
94 launcher_demonstrator()
98 which=$(($RANDOM % 3))
99 #hmmm: not asynch yet! make it so!
106 # we have reached the limit on processes and need to wait for a few, defined by
107 # procs_to_await variable at top.
108 if (($bg_count > $max_bg_procs - 1)); then
109 echo "have reached $max_bg_procs background processes threshold; waiting for $procs_to_await of them to complete."
110 wait_on_backgrounders $procs_to_await
116 launcher_demonstrator;