]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.service
Makefile: Fix typo in localstatedir
[people/ms/network.git] / src / functions / functions.service
CommitLineData
1eec4672
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
1c6a4e30 22service_start() {
1eec4672 23 local name=${1}
1eec4672
MT
24 assert isset name
25
3f3f9ea9
MT
26 # Don't start anything if this is already running
27 if service_is_active "${name}"; then
28 return ${EXIT_OK}
29 fi
30
31 systemctl start "${name}"
4243f0ca
SS
32
33 # Check, if the service was successfully started and
34 # return a proper exit code.
3f3f9ea9 35 service_is_active "${name}"
4243f0ca
SS
36 local ret=$?
37
1d08b9b3 38 log INFO "Started service '${name}', code=${ret}."
4243f0ca
SS
39
40 return ${ret}
1eec4672
MT
41}
42
1c6a4e30 43service_stop() {
3f3f9ea9 44 local name="${1}"
1eec4672
MT
45 assert isset name
46
3f3f9ea9 47 systemctl stop "${name}"
1eec4672
MT
48}
49
1c6a4e30 50service_restart() {
3f3f9ea9 51 local name="${1}"
1eec4672
MT
52 assert isset name
53
3f3f9ea9 54 systemctl restart "${name}"
1eec4672
MT
55}
56
1c6a4e30 57service_reload() {
3f3f9ea9 58 local name="${1}"
1eec4672
MT
59 assert isset name
60
3f3f9ea9
MT
61 if service_status "${name}"; then
62 systemctl reload "${name}"
1eec4672
MT
63 return $?
64 else
65 log WARNING "Cannot reload service '${name}' which is currently not running."
66 fi
67}
68
1c6a4e30 69service_status() {
3f3f9ea9 70 local name="${1}"
1eec4672
MT
71 assert isset name
72
3f3f9ea9 73 systemctl status "${name}" >/dev/null 2>&1
1eec4672
MT
74 return $?
75}
4243f0ca 76
5c5b8e36
SS
77# This function calls the "enable" command from systemd,
78# to mark services to be automatically started during
79# boot up.
1c6a4e30 80service_enable() {
3f3f9ea9 81 local name="${1}"
5c5b8e36
SS
82 assert isset name
83
84 systemctl enable "${name}" >/dev/null 2>&1
85}
86
87# This function calls the "disable" command of systemd,
88# to drop the autostart ability of the service during the
89# boot up.
1c6a4e30 90service_disable() {
3f3f9ea9 91 local name="${1}"
5c5b8e36
SS
92 assert isset name
93
94 systemctl disable "${name}" >/dev/null 2>&1
95}
96
97# This function uses the systemd command "is-enabled" to check,
98# if a service has been enabled or not.
1c6a4e30 99service_is_enabled() {
5c5b8e36
SS
100 local name="${1}"
101 assert isset name
102
103 systemctl is-enabled "${name}" >/dev/null 2>&1
104 return $?
105}
106
1c6a4e30 107service_is_active() {
3f3f9ea9 108 local name="${1}"
4243f0ca
SS
109 assert isset name
110
3f3f9ea9 111 systemctl is-active "${name}" >/dev/null 2>&1
4243f0ca
SS
112 return $?
113}
81d0c0b9 114
1c6a4e30 115service_get_exitcode() {
81d0c0b9
MT
116 local name=${1}
117 assert isset name
118
3f3f9ea9 119 local output=$(systemctl show "${name}" --property="ExecMainStatus")
81d0c0b9
MT
120 cli_get_val "${output}"
121}