-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathsystemd.py
93 lines (68 loc) · 2 KB
/
systemd.py
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
"""
Wraps systemctl to install, uninstall, start & stop systemd services.
If we use a debian package instead, we can get rid of all this code.
"""
import os
import subprocess
def reload_daemon():
"""
Equivalent to systemctl daemon-reload.
Makes systemd discover new units.
"""
subprocess.run(["systemctl", "daemon-reload"], check=True)
def install_unit(name, unit, path="/etc/systemd/system"):
"""
Install unit with given name
"""
with open(os.path.join(path, name), "w") as f:
f.write(unit)
def uninstall_unit(name, path="/etc/systemd/system"):
"""
Uninstall unit with given name
"""
subprocess.run(["rm", os.path.join(path, name)], check=True)
def start_service(name):
"""
Start service with given name.
"""
subprocess.run(["systemctl", "start", name], check=True)
def stop_service(name):
"""
Start service with given name.
"""
subprocess.run(["systemctl", "stop", name], check=True)
def restart_service(name):
"""
Restart service with given name.
"""
subprocess.run(["systemctl", "restart", name], check=True)
def enable_service(name):
"""
Enable a service with given name.
This most likely makes the service start on bootup
"""
subprocess.run(["systemctl", "enable", name], check=True)
def disable_service(name):
"""
Enable a service with given name.
This most likely makes the service start on bootup
"""
subprocess.run(["systemctl", "disable", name], check=True)
def check_service_active(name):
"""
Check if a service is currently active (running)
"""
try:
subprocess.run(["systemctl", "is-active", name], check=True)
return True
except subprocess.CalledProcessError:
return False
def check_service_enabled(name):
"""
Check if a service is enabled
"""
try:
subprocess.run(["systemctl", "is-enabled", name], check=True)
return True
except subprocess.CalledProcessError:
return False