This repository was archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit
executable file
·107 lines (100 loc) · 2.53 KB
/
init
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
#!/bin/bash
#
# Startup script for BTSync
#
# chkconfig: 345 80 30
# description: Starts the btsync daemon for configured users.
### BEGIN INIT INFO
# Provides: btsync
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Multi-user daemonized version of btsync.
# Description: Starts the btsync daemon for users listed in /etc/btsync_users (one user per line).
### END INIT INFO
config=/etc/btsync_users
daemon=/usr/local/bin/btsync
name="BTSync"
start() {
[ -x $daemon ] || exit 5
[ -f $config ] || { echo "$config does not exist"; exit 1; }
users=$(cat $config)
for btsuser in ${users[@]}; do
HOMEDIR=$(getent passwd $btsuser | cut -d: -f6)
pidfile="$HOMEDIR/.sync/sync.pid"
if [ -f $pidfile ]; then
if ps -p $(cat $pidfile) >/dev/null; then
echo "$name for $btsuser is already running"
continue
fi
fi
user_config="$HOMEDIR/.sync/config.json"
if [ -f $user_config ]; then
echo "Starting $name for $btsuser"
su - $btsuser -c "$daemon --config $user_config"
else
echo "Couldn't start $name for $btsuser (no $user_config found)"
fi
done
}
stop() {
[ -x $daemon ] || exit 5
[ -f $config ] || { echo "$config does not exist"; exit 1; }
users=$(cat $config)
for btsuser in ${users[@]}; do
HOMEDIR=$(getent passwd $btsuser | cut -d: -f6)
pidfile="$HOMEDIR/.sync/sync.pid"
if [ ! -f $pidfile ]; then
echo "$name for $btsuser is not running"
continue
fi
pid=$(cat $pidfile)
if [ ! -z "$pid" ]
then
echo "Stopping $name for $btsuser"
kill $pid 2>/dev/null
rm -f $pidfile
fi
done
}
status() {
[ -x $daemon ] || exit 5
[ -f $config ] || { echo "$config does not exist"; exit 1; }
users=$(cat $config)
for btsuser in $users; do
HOMEDIR=$(getent passwd $btsuser | cut -d: -f6)
pidfile="$HOMEDIR/.sync/sync.pid"
if [ ! -f $pidfile ]; then
echo "$name for $btsuser is not running"
continue
fi
pid=$(cat $pidfile)
if ps -p $pid >/dev/null; then
echo "$name for $btsuser: running (pid $pid)"
else
echo "$name for $btsuser: not running."
fi
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: service btsync start|stop|restart|status"
exit 1
esac
exit $?