9 pvalve - Control how much a given command should run by an other command's exit code
13 pvalve [<B<CONTROL COMMAND>>] -- [<B<LONG RUNNING COMMAND>>]
15 Controls when B<LONG RUNNING COMMAND> should run, by pause and unpause it
16 according to the B<CONTROL COMMAND>'s exit status.
20 Pause B<LONG RUNNING COMMAND> process group with STOP signal(7) if B<CONTROL COMMAND> exits non-zero.
21 Unpause B<LONG RUNNING COMMAND> process group with CONT signal(7) if B<CONTROL COMMAND> exits zero.
23 Pvalve takes the last line from B<CONTROL COMMAND>'s stdout, and if looks like a time interval
24 (ie. positive number with optional fraction followed by optional "s", "m", or "h" suffix)
25 then the next checking of B<CONTROL COMMAND> will start after that much time.
26 Otherwise it takes B<PVALVE_INTERVAL> environment variable,
27 or start next check immediately if it's not set.
29 Pvalve won't bombard B<LONG RUNNING COMMAND> with more consecutive STOP or CONT signals.
33 It's useful eg. for basic load control. Start a CPU-intensive program in B<LONG RUNNING COMMAND>
34 and check hardware temperature in B<CONTROL COMMAND>. Make it exit 0 when temperature is below
35 a certain value, and exit 1 if above an other, higher value.
41 =item B<PVALVE_INTERVAL>
43 Default interval between two B<CONTROL COMMAND> runs.
45 =item B<PVALVE_STATUS>
47 B<PVALVE_STATUS> describes whether B<LONG RUNNING COMMAND> should be in running or in paused state.
48 Possible values: RUN, STOP.
49 This environment is available by B<CONTROL COMMAND>.
53 PID of B<LONG RUNNING COMMAND>.
59 Further process groups which are created by B<LONG RUNNING COMMAND> will not be affected.
68 declare -a controller_cmd
69 declare -a controlled_cmd
85 controller_cmd
+=("$1")
91 controlled_cmd
+=("$1")
97 kill -TERM -$PVALVE_PID
98 if [ "$PVALVE_STATUS" = STOP
]
100 kill -CONT -$PVALVE_PID
108 declare -g ancestor_valid
109 if [ "$ancestor_valid" != no
]
111 kill -0 $pid 2>/dev
/null
&& return 0 || ancestor_valid
=no
113 kill -0 -$pid 2>/dev
/null
119 setpgrp
"${controlled_cmd[@]}" &
125 echo "$toolname: RUN $PVALVE_PID" >&2
127 stdout_file
=`readlink /proc/self/fd/1`
128 exec {stdout_copy_fd
}> "$stdout_file"
130 while exists
$PVALVE_PID
132 delay
=`command "${controller_cmd[@]}" | tee /dev/fd/$stdout_copy_fd | tail -n1; exit ${PIPESTATUS[0]}`
135 if [ $code = 0 -a $PVALVE_STATUS != RUN
]
137 echo "$toolname: CONT -$PVALVE_PID" >&2
138 kill -CONT -$PVALVE_PID
140 elif [ $code != 0 -a $PVALVE_STATUS = RUN
]
142 echo "$toolname: STOP -$PVALVE_PID" >&2
143 kill -STOP -$PVALVE_PID
147 if [[ $delay =~ ^
[0-9]+([.
,][0-9]+)?
[smh
]?$
]]
151 delay
=${PVALVE_INTERVAL:-0}
154 echo "$toolname: next check in $delay sec" >&2