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
114
115
116
|
#!/bin/sh
prog=$0
usage() {
echo "usage: rc-service [OPTS] SERVICE [ACTION]"
exit $1
}
msg() {
if ! [ -n "$quiet" ]; then
echo "$@"
fi
}
OPTS=$(getopt -l help,exists,ifexists,ifstarted,ifstopped,verbose,quiet \
-o "heiNsSvq" -n $prog -- "$@") || usage "1" >&2
# -e, --exists <arg> tests if the service exists or not
# -i, --ifexists if the service exists run the command
# -N, --ifnotstarted if the service is not started run the command
# -s, --ifstarted if the service is started run the command
# -S, --ifstopped if the service is stopped run the command
# -v, --verbose Run verbosely
# -q, --quiet Run quietly (repeat to suppress errors)
eval set -- "$OPTS"
for opt; do
case "$opt" in
-h|--help)
usage 0
;;
--exists)
exists=1
;;
--ifexists)
ifexists=1
;;
--ifstarted)
ifstarted=1
;;
--ifstopped)
ifstopped=1
;;
--verbose)
verbose=1
;;
--quiet)
quiet=1
;;
--)
shift
break
;;
esac
shift
done
svc="$1"
action="$2"
if [ -z "$svc" ]; then
usage "1" >&2
fi
if [ -z "$action" ] && [ -z "$exists" ]; then
usage "1" >&2
fi
if [ -n "$exists" ]; then
test -e "$ROOT"/etc/init.d/$svc
exit
fi
if [ -n "$ifstarted" ] && ! [ -e "$ROOT"/run/started/$svc ]; then
exit 0
fi
if [ -n "$ifstopped" ] && [ -e "$ROOT"/run/started/$svc ]; then
exit 0
fi
case "$action" in
start)
if [ -e "$ROOT"/run/started/$svc ]; then
! [ -n "$ifstopped" ] && msg " * WARNING: $svc has already been started" >&2
exit 0
fi
msg " * Starting $svc ... [ok]"
mkdir -p "$ROOT"/run/started
touch "$ROOT"/run/started/$svc
;;
stop)
if ! [ -e "$ROOT"/run/started/$svc ]; then
msg " * WARNING: $svc is already stopped" >&2
exit 0
fi
msg "* Stopping $svc ... [ok]"
rm -f "$ROOT"/run/started/$svc
;;
restart)
$prog --ifstarted $svc stop && $prog $svc start
;;
status)
if [ -e "$ROOT"/run/started/$svc ]; then
msg " * status: started"
exit 0
else
msg " * status: stopped"
exit 3
fi
;;
*) usage 1
esac
|