summaryrefslogtreecommitdiff
path: root/ext/installfiles/linux/init.d/zerotier-one
blob: b0a8aa4184b7a3780feb233b046236412d13b674 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh
#
# zerotier-one   Virtual distributed Ethernet service
#
# chkconfig: 2345 11 89
# description: ZeroTier One provides public and private distributed ethernet \
#              networks. See https://www.zerotier.com/ for more information.

### BEGIN INIT INFO
# Provides:          zerotier-one
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start ZeroTier One
# Description:       ZeroTier One provides public and private distributed ethernet \
#                    networks. See https://www.zerotier.com/ for more information.
### END INIT INFO

#
# This script is written to avoid distro-specific dependencies, so it does not
# use the rc bash script libraries found on some systems. It should work on
# just about anything.
#

zthome=/var/lib/zerotier-one

# Add $zthome to path so we can invoke zerotier-one naked, makes it look
# better in a ps listing.
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:$zthome

if [ "`id -u`" -ne 0 ]; then
	echo "Init script must be called as root."
	exit 4
fi

if [ ! -f "$zthome/zerotier-one" ]; then
	echo "ZeroTier One is not installed in $zthome."
	exit 5
fi

pid=0
if [ -f "$zthome/zerotier-one.pid" ]; then
	pid=`cat $zthome/zerotier-one.pid`
fi

running=0
if [ "$pid" -gt 0 ]; then
	exepath=`readlink /proc/$pid/exe 2>/dev/null | grep zerotier-one`
	if [ -n "$exepath" ]; then
		running=1
	fi
fi

case "$1" in
	start)
		if [ $running -gt 0 ]; then
			echo "ZeroTier One already running."
			exit 0
		fi
		echo "Starting ZeroTier One..."
		zerotier-one -d
		;;
	stop)
		if [ $running -gt 0 ]; then
			echo "Stopping ZeroTier One..."
			kill -TERM $pid
			sleep 0.25
			if [ -f "$zthome/zerotier-one.pid" ]; then
				sleep 0.5
			fi
			if [ -f "$zthome/zerotier-one.pid" ]; then
				sleep 1
			fi
			if [ -f "$zthome/zerotier-one.pid" ]; then
				kill -KILL $pid >>/dev/null 2>&1
				rm -f "$zthome/zerotier-one.pid"
			fi
		else
			echo "ZeroTier One is not running."
		fi
		;;
	restart|reload|force-reload|condrestart|try-restart)
		echo "Restarting ZeroTier One..."
		if [ $running -gt 0 ]; then
			kill -TERM $pid >>/dev/null 2>&1
		fi
		sleep 0.25
		if [ -f "$zthome/zerotier-one.pid" ]; then
			sleep 0.5
		fi
		if [ -f "$zthome/zerotier-one.pid" ]; then
			sleep 1
		fi
		if [ -f "$zthome/zerotier-one.pid" ]; then
			kill -KILL $pid >>/dev/null 2>&1
			rm -f "$zthome/zerotier-one.pid"
		fi
		zerotier-one -d
		;;
	status)
		if [ $running -gt 0 ]; then
			exit 0
		else
			exit 3
		fi
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
		exit 2
esac

exit 0