rtomayko / hem

persistent ssh connection and tunnel manager

This URL has Read+Write access

hem / hem-down.sh
100644 59 lines (49 sloc) 1.088 kb
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
54
55
56
57
58
59
#!/bin/sh
set -e
USAGE="[-s <sig>] <profile>...
Take down one or more connections or send them a specific kill signal.
 
-s, --signal <sig> Take process down with signal <sig>.
--help Display usage and exit.
 
This command exits with a status of 1 when the process does not come
down properly."
 
. hem-sh-setup
 
# parse arguments
sig="-TERM"
while [ $# -gt 0 ]; do
case "$1" in
-s|--signal)
[ $# -lt 2 ] &&
see_usage "the --signal argument requires a value."
sig="$2"
shift; shift
;;
-*)
see_usage "invalid argument: $1"
;;
*)
break
;;
esac
done
 
test $(expr -- "$sig" : '\-') = 1 ||
sig="-$sig"
 
# bring in profile settings
profile_required "$@"
 
# check that connection isn't already running.
pid=$(hem-status --pid "$profile_name")
if [ -z "$pid" ] ; then
info "$profile_name is already down"
exit 2
fi
 
message="taking down: $profile_name (pid: $pid)"
test "$sig" = "-TERM" ||
message="$message with $sig"
info "$message"
 
command="kill $sig $pid"
log "$command"
if $command ; then
exit 0
else
result=$?
die "kill failed with $result"
fi