-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_startup_service
More file actions
70 lines (55 loc) · 2.14 KB
/
add_startup_service
File metadata and controls
70 lines (55 loc) · 2.14 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
#! /usr/bin/python3
import os
import os.path as osp
import argparse
def get_args():
parser = argparse.ArgumentParser("add startup service")
parser.add_argument("-n", "--name", type=str, default=None)
parser.add_argument("-c", "--command", required=True)
parser.add_argument("--comment", type=str, default=None)
parser.add_argument("--network", action="store_true", help="rely on network")
parser.add_argument("--no-start", action="store_true", help="do not add to startup service, just create service file")
return parser.parse_args()
def create_service_file(args):
if args.name is None:
for c in args.command.split():
if osp.isfile(c):
args.name = osp.basename(c).split(".bash")[0].split(".sh")[0].split(".py")[0]
break
if args.name is None:
print("please enter a name for this service!")
exit(-1)
if args.comment is None:
args.comment = args.name
net_config = """Requires=network-online.target
After=network-online.target
""" if args.network else "\n"
content = f"""[Unit]
Description={args.comment}
{net_config}
[Service]
Type=forking
ExecStart={args.command}
[Install]
WantedBy=multi-user.target"""
file_name = args.name + ".service"
with open(file_name, "w") as f:
f.write(content)
if osp.isfile(file_name):
print(f"create {file_name}")
return file_name, content
def add2startService(filename: str):
os.system(f"sudo cp {filename} /lib/systemd/system/")
service_file = osp.join("/lib/systemd/system/", filename)
if osp.isfile(service_file):
for command in [f"sudo chmod +x {service_file}", "sudo systemctl daemon-reload", f"sudo systemctl enable {filename}"]:
print("exec: ", command)
os.system(command)
print("use 'systemctl status " + filename[:-8] + "' to check service status")
else:
print("failed to create service file in /lib/systemd/system, please run this script by using root user")
if __name__ == "__main__":
args = get_args()
f, c = create_service_file(args)
if not args.no_start:
add2startService(f)