forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemctl
executable file
·281 lines (211 loc) · 7.17 KB
/
systemctl
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
function get_unit_file(){
local UNIT=$1
for DIR in ${UNIT_PATHS[@]} ; do
if [ -f "${DIR}${UNIT}" ] ; then
echo "${DIR}${UNIT}"
break
fi
done
}
function read_option(){
local OPTION="$1"
local UNIT_FILE="$2"
local UNIT_INSTANCE="$3"
local UNIT=`basename $UNIT_FILE`
local UNIT_FULL=`echo $UNIT | sed "s/@/@$UNIT_INSTANCE/"`
VALUE="$(grep '^'$OPTION'[= ]' "$UNIT_FILE" | cut -d '=' -f2- | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
VALUE="`
echo $VALUE |
sed -e "s/%[i]/$UNIT_INSTANCE/g" \
-e "s/%[I]/\"$UNIT_INSTANCE\"/g" \
-e "s/%[n]/$UNIT_FULL/g" \
-e "s/%[N]/\"$UNIT_FULL\"/g"
`"
# TODO: Add more options from:
# https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers
echo $VALUE
}
function get_unit_wants() {
local UNIT_FILE=$1
local UNIT=`basename $UNIT_FILE`
sort -u <<< `(
# Print wants from UNIT_PATHS
for DIR in ${UNIT_PATHS[@]} ; do
if [ -d "${DIR}${UNIT}.wants" ] ; then
ls -1 "${DIR}${UNIT}.wants/" | tr '\n' ' '
fi
done
# Print wants from unit-file
read_option Wants $UNIT_FILE
)`
}
function action_start(){
# Find depended services
local UNIT_FILE=$1
local UNIT_WANTS=(`get_unit_wants $1`)
local UNIT_INSTANCE=$2
# Start depended services
for UNIT in ${UNIT_WANTS[@]}; do
exec_action start $UNIT
done
# Load options
local User=`read_option User $UNIT_FILE $UNIT_INSTANCE`
local Type=`read_option Type $UNIT_FILE $UNIT_INSTANCE`
local EnvironmentFile=`read_option EnvironmentFile $UNIT_FILE $UNIT_INSTANCE`
local ExecStartPre=(`read_option ExecStartPre $UNIT_FILE $UNIT_INSTANCE`)
local ExecStart=`read_option ExecStart $UNIT_FILE $UNIT_INSTANCE`
local ExecStartPost=(`read_option ExecStartPost $UNIT_FILE $UNIT_INSTANCE`)
local Restart=(`read_option Restart $UNIT_FILE $UNIT_INSTANCE`)
local RestartSec=(`read_option RestartSec $UNIT_FILE $UNIT_INSTANCE`)
RestartSec=${RestartSec:=5}
[ -f "$EnvironmentFile" ] && source "$EnvironmentFile"
# Start service
if [ -z $Type ] || [[ "${Type,,}" == *"simple"* ]] ; then
if [ "$Restart" == "always" ]; then
COMMAND='nohup bash -c "while true ; do '"$ExecStart"'; sleep $RestartSec; done" &>/dev/null &'
else
COMMAND='nohup '"$ExecStart"' >>/dev/null 2>&1 &'
fi
elif [[ "${Type,,}" == *"forking"* ]] || [[ "${Type,,}" == *"oneshot"* ]] ; then
COMMAND="$ExecStart"
else
>&2 echo "Unknown service type $Type"
fi
#[ -z $User ] || COMMAND="su $User -c \"$COMMAND\""
while IFS=$'\n' read -a i; do
eval $i
done <<< "${ExecStartPre[@]}"
eval "$COMMAND"
while IFS=$'\n' read -a i; do
eval $i
done <<< "${ExecStartPost[@]}"
}
function action_stop(){
# Find depended services
local UNIT_FILE=$1
local UNIT_WANTS=(`get_unit_wants $1`)
local UNIT_INSTANCE=$2
# Load options
local User=`read_option User $UNIT_FILE $UNIT_INSTANCE`
local Type=`read_option Type $UNIT_FILE $UNIT_INSTANCE`
local EnvironmentFile=`read_option EnvironmentFile $UNIT_FILE $UNIT_INSTANCE`
local ExecStopPre=(`read_option ExecStartPre $UNIT_FILE $UNIT_INSTANCE`)
local ExecStop=`read_option ExecStop $UNIT_FILE $UNIT_INSTANCE`
local ExecStopPost=(`read_option ExecStartPost $UNIT_FILE $UNIT_INSTANCE`)
local ExecStart=`read_option ExecStart $UNIT_FILE $UNIT_INSTANCE`
[ -f "$EnvironmentFile" ] && source "$EnvironmentFile"
# Stop service
if [ -z $ExecStop ] ; then
COMMAND="kill -TERM \$(pgrep -f \"$ExecStart\")"
else
COMMAND="$ExecStop"
fi
#[ -z $User ] || COMMAND="su $User -c \"$COMMAND\""
while IFS=$'\n' read -a i; do
eval $i
done <<< "${ExecStopPre[@]}"
eval "$COMMAND"
while IFS=$'\n' read -a i; do
eval $i
done <<< "${ExecStopPost[@]}"
}
function action_restart(){
local UNIT_FILE=$1
local UNIT_INSTANCE=$2
action_stop $UNIT_FILE $UNIT_INSTANCE
action_start $UNIT_FILE $UNIT_INSTANCE
}
function action_enable(){
local UNIT_FILE=$1
local UNIT=`basename $UNIT_FILE`
local UNIT_INSTANCE=$2
local UNIT_FULL=`echo $UNIT | sed "s/@/@$UNIT_INSTANCE/"`
local WantedBy=`read_option WantedBy $UNIT_FILE`
if [ -z $WantedBy ] ; then
>&2 echo "Unit $UNIT have no WantedBy option."
exit 1
fi
local WANTEDBY_DIR="/etc/systemd/system/$WantedBy.wants"
if [ ! -f "$WANTEDBY_DIR/$UNIT_FULL" ] ; then
mkdir -p $WANTEDBY_DIR
echo Created symlink from $WANTEDBY_DIR/$UNIT_FULL to $UNIT_FILE.
ln -s $WANTEDBY_DIR/$UNIT_FULL $UNIT_FILE
fi
}
function action_disable(){
local UNIT_FILE=$1
local UNIT=`basename $UNIT_FILE`
local UNIT_INSTANCE=$2
local UNIT_FULL=`echo $UNIT | sed "s/@/@$UNIT_INSTANCE/"`
local WantedBy=`read_option WantedBy $UNIT_FILE`
if [ -z $WantedBy ] ; then
>&2 echo "Unit $UNIT have no WantedBy option."
exit 1
fi
local WANTEDBY_DIR="/etc/systemd/system/$WantedBy.wants"
if [ -f "$WANTEDBY_DIR/$UNIT_FULL" ] ; then
echo Removed $WANTEDBY_DIR/$UNIT_FULL.
rm -f $WANTEDBY_DIR/$UNIT_FULL.
rmdir --ignore-fail-on-non-empty $WANTEDBY_DIR
fi
}
function action_status(){
# Find depended services
local UNIT_FILE=$1
local UNIT_WANTS=(`get_unit_wants $1`)
local UNIT_INSTANCE=$2
local ExecStart=`read_option ExecStart $UNIT_FILE $UNIT_INSTANCE`
COMMAND="pgrep -f \"$ExecStart\" &>/dev/null"
if eval "$COMMAND"; then
exit 0
fi
>&2 echo "Loaded: not-found"
exit 1
}
function action_is_enabled(){
exit 0
}
function action_is_active(){
local UNIT=`basename $1`
if systemctl status $UNIT ; then
>&2 echo "active"
exit 0
fi
exit 1
}
function exec_action(){
local ACTION=$1
local UNIT=$2
[[ $UNIT =~ '.' ]] || UNIT="$UNIT.service"
if [[ $UNIT =~ '@' ]] ; then
local UNIT_INSTANCE=`echo $UNIT | cut -d'@' -f2- | cut -d. -f1`
local UNIT=`echo $UNIT | sed "s/$UNIT_INSTANCE//"`
fi
UNIT_FILE=`get_unit_file $UNIT`
if [ -z $UNIT_FILE ] ; then
>&2 echo "Failed to $ACTION $UNIT: Unit $UNIT not found."
exit 1
else
case "$ACTION" in
start ) action_start $UNIT_FILE $UNIT_INSTANCE ;;
stop ) action_stop $UNIT_FILE $UNIT_INSTANCE ;;
restart ) action_restart $UNIT_FILE $UNIT_INSTANCE ;;
enable ) action_enable $UNIT_FILE $UNIT_INSTANCE ;;
disable ) action_disable $UNIT_FILE $UNIT_INSTANCE ;;
status ) action_status $UNIT_FILE $UNIT_INSTANCE ;;
is-enabled ) action_is_enabled $UNIT_FILE $UNIT_INSTANCE ;;
is-active ) action_is_active $UNIT_FILE $UNIT_INSTANCE ;;
* ) >&2 echo "Unknown operation $ACTION." ; exit 1 ;;
esac
fi
}
ACTION="$1"
UNITS="${@:2}"
UNIT_PATHS=(
/etc/systemd/system/
/usr/lib/systemd/system/
)
for UNIT in ${UNITS[@]}; do
exec_action $ACTION $UNIT
done