-
Notifications
You must be signed in to change notification settings - Fork 4
/
sshgateway.sh
executable file
·54 lines (48 loc) · 1.39 KB
/
sshgateway.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
DEST="$1"
persistcommand() {
# If command takes less then LIMIT seconds, don't execute it again
LIMIT=2
# Wait DELAY seconds before running the command again
DELAY=15
FORCE="n"
if [ "$1" = "-f" -o "$1" = "--force" ]; then
FORCE="y"
shift
fi
while :; do
echo "Starting persistcommand:($*). Delay = $DELAY, limit = $LIMIT, force = $FORCE "
date
startcommand=`date +%s`
"$@"
EXITCODE=$?
endcommand=`date +%s`
secondsran=$((endcommand-startcommand))
days=$((secondsran/86400))
secondsran=$((secondsran-(days*86400)))
hours=$((secondsran/3600))
secondsran=$((secondsran-(hours*3600)))
mins=$((secondsran/60))
secondsleft=$((secondsran-(mins*60)))
date
echo "The command ran for [${days}d:${hours}h:${mins}m:${secondsleft}s]"
if [ $EXITCODE -eq 0 ]; then
if [ "$FORCE" = "y" ]; then
echo "Seems like a normal exit, but forcing a resume of persistcommand:($*)."
else
echo "Seems like a normal exit, quitting persistcommand:($*)."
break
fi
else
echo "Exitcode was: $EXITCODE."
fi
if [ "$secondsran" -gt $LIMIT -o "$FORCE" = "y" ]; then
echo "Running the command again after $DELAY seconds..."
sleep $DELAY
else
echo "Command ran for less than $LIMIT seconds. Not trying it again. Exiting..."
break
fi
done
}
persistcommand -f ssh $DEST