forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceControl.java
More file actions
129 lines (126 loc) · 3.94 KB
/
Copy pathServiceControl.java
File metadata and controls
129 lines (126 loc) · 3.94 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
package javaforce.linux;
/** Service Control (systemctl)
*
* @author pquiring
*/
import javaforce.*;
public class ServiceControl {
public static boolean start(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "start", name}, true);
JFLog.log(output);
return sp.getErrorLevel() == 0;
}
public static boolean stop(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "stop", name}, true);
JFLog.log(output);
return sp.getErrorLevel() == 0;
}
public static boolean restart(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "restart", name}, true);
JFLog.log(output);
return sp.getErrorLevel() == 0;
}
public static boolean enable(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "enable", name}, true);
JFLog.log(output);
return sp.getErrorLevel() == 0;
}
public static boolean disable(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "disable", name}, true);
JFLog.log(output);
return sp.getErrorLevel() == 0;
}
public static boolean isEnabled(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "status", name}, true);
JFLog.log(output);
boolean enabled = false;
boolean active = false;
String[] lns = output.split("\n");
for(String ln : lns) {
ln = ln.trim();
if (ln.startsWith("Loaded:")) {
int i1 = ln.indexOf(';');
int i2 = ln.indexOf(';', i1 + 1);
String state = ln.substring(i1 + 1, i2);
enabled = state.trim().equals("enabled");
continue;
}
if (ln.startsWith("Active:")) {
ln = ln.substring(7).trim();
active = ln.startsWith("active");
continue;
}
}
return enabled;
}
public static boolean isActive(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "status", name}, true);
JFLog.log(output);
boolean enabled = false;
boolean active = false;
String[] lns = output.split("\n");
for(String ln : lns) {
ln = ln.trim();
if (ln.startsWith("Loaded:")) {
int i1 = ln.indexOf(';');
int i2 = ln.indexOf(';', i1 + 1);
String state = ln.substring(i1 + 1, i2);
enabled = state.trim().equals("enabled");
continue;
}
if (ln.startsWith("Active:")) {
ln = ln.substring(7).trim();
active = ln.startsWith("active");
continue;
}
}
return active;
}
/** return = [name, enabled, active] */
public static String[] getStates(String name) {
ShellProcess sp = new ShellProcess();
String output = sp.run(new String[] {"/usr/bin/systemctl", "status", name}, true);
JFLog.log(output);
String enabled = "n/a";
String active = "n/a";
String[] lns = output.split("\n");
for(String ln : lns) {
ln = ln.trim();
if (ln.startsWith("Loaded:")) {
int i1 = ln.indexOf(';');
int i2 = ln.indexOf(';', i1 + 1);
if (i2 == -1) {
i2 = ln.indexOf(')', i1 + 1);
}
String state = ln.substring(i1 + 1, i2).trim();
switch (state) {
case "enabled": enabled = "true"; break;
case "disabled": enabled = "false"; break;
default: enabled = state; break;
}
continue;
}
if (ln.startsWith("Active:")) {
ln = ln.substring(7).trim();
if (ln.startsWith("active")) {
active = "true";
} else {
active = "false";
}
continue;
}
}
String[] states = new String[3];
states[0] = name;
states[1] = enabled;
states[2] = active;
return states;
}
}