Commit 3b809975 authored by Tommi's avatar Tommi

initial commit

parents
# keepalive.sh
keepalive.sh is yet another script that unconditionally attempts to keep a
process alive.
## Quick start
```sh
% sh keepalive.sh command
```
This will launch `command` and restart it should it exit.
## Getting started
### Source
keepalive.sh is hosted at GitLab, https://gitlab.com/tsaviran/keepalive .
### Dependencies
* Any Bourne shell
* Standard Unix tools: tr, head, sed, expr, sleep
## Usage
```
Usage: keepalive.sh ARG ...
env can be
FORK=y fork process
BACKOFF_INIT=2 initial backoff time in seconds
BACKOFF_MULTI=2.0 multiply backoff with f on failure
BACKOFF_RAND=1.0 multiplier for backoff randomness
BACKOFF_LIMIT=300 backoff time upper limit in seconds
BACKOFF_MIN=2 minimum backoff time in seconds
LIFE_EXPECTANCY=15 expected life time of a process in seconds
DEBUG= enable debugging
```
### Examples
```sh
% FORK=y sh keepalive.sh autossh tunnel -N
```
While autossh is an excellent tool, it exits on 0 return code. keepalive.sh can
be used to address this feature.
#! /bin/sh
set -e
usage() {
cat <<USAGE
Usage: $0 ARG ...
env can be
FORK=y fork process
BACKOFF_INIT=2 initial backoff time in seconds
BACKOFF_MULTI=2.0 multiply backoff with f on failure
BACKOFF_RAND=1.0 multiplier for backoff randomness
BACKOFF_LIMIT=300 backoff time upper limit in seconds
BACKOFF_MIN=2 minimum backoff time in seconds
LIFE_EXPECTANCY=15 expected life time of a process in seconds
DEBUG= enable debugging
USAGE
}
debug() {
test "$DEBUG" || return 0
echo "$@" >&2
}
rand_m() {
m="$(tr -cd 0-9 </dev/urandom | head -c3 | sed 's/^0*//')"
expr $m + 1000
}
to_milli() {
echo $1 | sed 'x; s/^/123/; x; :l x; /./ !{x; b p}; s/.//; x; /\../ { s/\.\(.\)/\1./; b l }; /\./ { s/\./0/; b l }; s/$/0/; b l; :p; s/\..*//'
}
if test $# -eq 0; then
usage
exit 0
fi
if test "$FORK"; then
unset FORK
export FORK
sleep 1 # prevent fork bomb :-/
sh "$0" "$@" &
exit 0
fi
# set defaults
test "$BACKOFF_INIT" || BACKOFF_INIT=5
test "$BACKOFF_MULTI" || BACKOFF_MULTI=2.0
test "$BACKOFF_RAND" || BACKOFF_RAND=1.0
test "$LIFE_EXPECTANCY" || LIFE_EXPECTANCY=15
BACKOFF_MULTI="$(to_milli $BACKOFF_MULTI)"
BACKOFF_RAND="$(to_milli $BACKOFF_RAND)"
debug init: $BACKOFF_INIT
debug multi: $BACKOFF_MULTI
debug rand: $BACKOFF_RAND
debug life: $LIFE_EXPECTANCY
backoff=$BACKOFF_INIT
while true; do
started_at="$(date +%s)"
"$@"
alive="$(expr $(date +%s) - $started_at)" || true
debug "process life: $alive, expected $LIFE_EXPECTANCY"
if test $alive -ge $LIFE_EXPECTANCY; then
backoff=$BACKOFF_INIT
sleep $backoff
continue
fi
r="$(rand_m)"
backoff="$(expr $backoff \* $r \* $BACKOFF_MULTI / 1000000)"
test $backoff -ge $BACKOFF_INIT || backoff=$BACKOFF_INIT
test $backoff -lt $BACKOFF_LIMIT || backoff=$BACKOFF_LIMIT
debug "Backoff now $backoff"
sleep $backoff
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment