Skip to content

Commit 4ba749d

Browse files
m0dulardonoghuc
authored andcommitted
(MAINT) Fix Case Statement Logic
This change breaks out service and initctl into their own case statements, as well as fixes a couple of minor bugs around trying to start or stop a service already in the desired state returning an error. Co-authored-by: m0dular <[email protected]> Co-authored-by: donoghuc <[email protected]>
1 parent 7976436 commit 4ba749d

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

tasks/linux.sh

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,37 @@ case "$available_manager" in
4545
fi
4646
;;
4747

48-
# These commands seem to only differ slightly in their invocation
49-
"service"|"initctl")
50-
if [[ $service == "service" ]]; then
51-
cmd=("$service" "$name" "$action")
52-
cmd_status=("$service" "$name" "status")
48+
"initctl")
49+
cmd=("$service" "$action" "$name")
50+
cmd_status=("$service" "status" "$name")
51+
52+
# The initctl show-config output has 'interesting' spacing/tabs, use word splitting to have single spaces
53+
word_split=($("$service" show-config "$name" 2>&1))
54+
enabled_out="${word_split[@]}"
55+
56+
if [[ $action != "status" ]]; then
57+
# service and initctl may return non-zero if the service is already started or stopped
58+
# If so, check for either "already running" or "Unknown instance" or "is not running" in the output before failing
59+
"${cmd[@]}" &>"$_tmp" || {
60+
grep -q "already running" "$_tmp" || grep -q "Unknown instance:" "$_tmp" || grep -q "is not running" "$_tmp" || fail
61+
}
62+
63+
cmd_out="$("${cmd_status[@]}" 2>&1)"
64+
success "{ \"status\": \"${cmd_out}\" }"
65+
fi
66+
67+
# "status" is already pretty terse for these commands
68+
cmd_out="$("${cmd_status[@]}" 2>&1)"
69+
success "{ \"status\": \"${cmd_out}\", \"enabled\": \"${enabled_out}\" }"
70+
;;
71+
72+
"service")
73+
cmd=("$service" "$name" "$action")
74+
cmd_status=("$service" "$name" "status")
75+
76+
# Several possibilities: chkconfig may be installed, the service may be a SysV job, or it may have been converted to Upstart
77+
# This is exactly why we have systemd now
78+
if type chkconfig &>/dev/null; then
5379
# The chkconfig output has 'interesting' spacing/tabs, use word splitting to have single spaces
5480
word_split=($(chkconfig --list "$name" 2>&1))
5581
else

0 commit comments

Comments
 (0)