-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice
More file actions
executable file
·135 lines (121 loc) · 3.42 KB
/
service
File metadata and controls
executable file
·135 lines (121 loc) · 3.42 KB
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
#!/bin/bash
command=${1}
service_name=${2}
invert=${3}
result=0
function serviceEnable() {
result=1
if [ -d "/etc/sv/${service_name}" ]; then
if [ ! -d "/var/service/${service_name}" ]; then
sudo ln -s /etc/sv/${service_name} /var/service/${service_name}
if [ -d "/var/service/${service_name}" ]; then
echo "Service ${service_name} enabled."
result=0
else
echo "Error during enable ${service_name}."
fi
else
echo "Service ${service_name} already enabled."
result=0
fi
else
echo "Service ${service_name} does not exists."
fi
}
function serviceDisable() {
result=1
if [ -d "/var/service/${service_name}" ]; then
sudo rm /var/service/${service_name}
if [ ! -d "/var/service/${service_name}" ]; then
echo "Service ${service_name} disabled."
result=0
else
echo "Error during disable ${service_name}."
fi
else
echo "Service ${service_name} already disabled."
result=0
fi
}
function serviceRestart() {
serviceStop
if [ ${result} -eq 0 ]; then
serviceStart
fi
}
function serviceStart() {
sudo sv up ${service_name}
result=${?}
}
function serviceStop() {
sudo sv down ${service_name}
result=${?}
}
function serviceStatus() {
sudo sv status ${service_name}
result=${?}
}
function serviceList() {
if [[ "${service_name}" == "enabled" ]]; then
for lname in $(ls -1 /var/service); do
service_name=${lname}
st_stat=$(serviceStatus | awk -F ":" '{print $1}' )
echo "${service_name}: ${st_stat}"
done
elif [[ "${service_name}" == "disabled" ]]; then
for lname in $(ls -1 /etc/sv/); do
if [ ! -d "/var/service/${lname}" ]; then
echo "${lname}"
fi
done
else
for lname in $(ls -1 /etc/sv/); do
if [ ! -d "/var/service/${lname}" ]; then
echo "${lname} - disabled"
else
echo "${lname} - enabled"
fi
done
fi
result=1
}
function showHelp() {
echo "Usage ${0} <command> <service_name>."
echo "If command is not in supported command list the program try to invert the syntax to ${0} <service_name> <command>."
echo "If also in this way the command is not present in supported command list you will receive an error message."
echo
echo "Supported commands:"
echo " - enable: enable a service, remember that with runit the enable command also start the service."
echo " - disable: disable a service, remember that with runit the disable command also stop the service."
echo " - restart: restart a service."
echo " - start: start a service, the service must be enabled."
echo " - stop: stop a service."
echo " - status: show the current service status."
echo " - list [enabled|disabled]: list the status of all services, if the string \"enabled\" or \"disabled\" is passed as parameter the script show only the services that are in the status specified."
echo " - help: print this help message."
result=1;
}
if [[ -z "${command}" ]]; then
command=help;
fi
case ${command} in
enable) serviceEnable;;
disable) serviceDisable;;
restart) serviceRestart;;
start) serviceStart;;
stop) serviceStop;;
status) serviceStatus;;
list) serviceList;;
help) showHelp;;
*)
if [[ "${invert}" == "false" ]]; then
echo "Error: Command \"${command}\" or \"${service_name}\" not found."
echo
showHelp
result=0
else
${0} ${service_name} ${command} "false"
fi
;;
esac
exit ${result}