forked from smaknsk/servicectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicectl
executable file
·202 lines (171 loc) · 4.62 KB
/
servicectl
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# Control services for system based on Systemd inside chroot and SysVinit outside chroot
# https://github.com/smaknsk/servicectl
#
# Pach systemd services file
SYSTEMD_UNITS_PATH="/lib/systemd/system/"
# Path contents symlink on systemd units files
SERVICECTL_ENABLED_PATH="/etc/init.d-chroot/"
# Check is root
if [ $EUID -ne 0 ]; then
echo "You must run as root user" 2>&1
exit 1
fi
# Parse ini file. usage: parse $inifile $section $key
# Return 0 on success, 1 if file cannot be opened, 2 if the given key cannot be found in the given section
function parse()
{
local _inifile=$1
local _section=$2
local _key=$3
if [ ! -r "$_inifile" ]
then
exit 1;
fi
exec < $_inifile
while read section; do
if [ "$section" = '['$_section']' ] ; then
IFS='='
while read key value; do
# check if we are still within our section
if [ `echo -n $key | grep "^\s*\[.*\]\s*$"` ]; then
exit 2;
fi
# strip leading and trailing whitespace from keys
key=`echo -n "$key" | sed 's/^\s*//;s/\s*$//'`
_key=`echo -n "$_key" | sed 's/^\s*//;s/\s*$//'`
if [ "$key" = "$_key" ]; then
echo $value
exit 0;
fi
done
fi
done
exit 2;
}
# Execute action from systemd service file
function exec_action() {
local action=$1
local service=$2
local file="${SYSTEMD_UNITS_PATH}${service}.service"
local is_required=1 # by default turn action is required
local cmd=""
# if passed arg $3 then set value
if [[ -n $3 ]]; then
local is_required=$3
fi
cmd=`parse $file Service $action`
local ret=$?
if [ $ret = 1 ]; then
echo "Error: file $file cannot be opened"
return 1
fi
if [ $ret = 2 ]; then
# if action required, return error
if [ $is_required = 1 ]; then
echo "Error: action $action not found in file $file"
return 1
fi
return 0
fi
eval $cmd
return $?
}
function exec_if_exists() {
exec_action $@ 0
}
function exec_stop() {
local service=$1
local file="${SYSTEMD_UNITS_PATH}${service}.service"
local cmd=""
cmd=`parse $file Service ExecStop`
local ret=$?
# if ExecStop exists
if [ $ret = 0 ]; then
exec_action ExecStop $service
return $?
fi
# get path pid file
pid_file=`parse $file Service PIDFile`
local ret=$?
if [ $ret != 0 ]; then
echo "Error: attribute PIDFile not exists in file $file"
return 1
fi
read PID < $pid_file
kill -TERM "$PID" || echo "Couldn't kill PID"
}
if [[ -z ${@:2} ]]; then
echo "Error: you must specify the service"
exit 1
fi
# Switch action
case "$1" in
start)
for service in ${@:2}
do
service=${service%".service"}
exec_if_exists ExecStartPre $service
if [ $? = 0 ]; then
exec_action ExecStart $service
fi
done
;;
stop)
for service in ${@:2}
do
service=${service%".service"}
exec_stop $service
done
;;
restart)
for service in ${@:2}
do
service=${service%".service"}
exec_stop $service
exec_action ExecStart $service
done
;;
reload)
for service in ${@:2}
do
service=${service%".service"}
exec_action ExecReload $service
done
;;
enable)
for service in ${@:2}
do
service=${service%".service"}
file="${SYSTEMD_UNITS_PATH}${service}.service"
enabled_symlink="${SERVICECTL_ENABLED_PATH}${service}.service"
if [ ! -f "$file" ]; then
echo "Error: file $file is not exists"
continue
fi
if [ -f "$enabled_symlink" ]; then
echo "${service} already enabled"
continue
fi
echo "ln -s \"$file\" \"$enabled_symlink\""
ln -s "$file" "$enabled_symlink"
done
;;
disable)
for service in ${@:2}
do
service=${service%".service"}
file="${SERVICECTL_ENABLED_PATH}${service}.service"
if [ ! -f "$file" ]; then
echo "Error: file $file is not exists"
continue
fi
echo "rm $file"
rm $file
done
;;
*)
echo "Available actions: start, stop, restart, reload, enable or disable"
exit 1
;;
esac