Scripts/create_systemd_service.py

54 lines
1.7 KiB
Python
Raw Normal View History

2024-04-17 16:19:11 -04:00
#!/bin/python3
2024-04-17 18:14:17 -04:00
import argparse
import subprocess
2024-04-29 17:13:26 -04:00
import util.color_io as color_io
2024-04-17 18:26:03 -04:00
import sys
2024-04-17 18:14:17 -04:00
parser = argparse.ArgumentParser(prog='SystemD Service Generator', description='SystemD Service Unit File Generator', epilog='Meow')
parser.add_argument("-i", "--install", nargs='?', const="/etc/systemd/system/", default=None)
2024-05-01 20:10:09 -04:00
parser.add_argument("-r", "--restart", default="on-failure")
parser.add_argument("-w", "--wanted", default="multi-user.target")
parser.add_argument("-d", "--working_dir", default=None)
parser.add_argument("-u", "--user", default=None)
parser.add_argument('-g', "--group", default=None)
2024-04-17 18:26:03 -04:00
parser.add_argument("service_name", required=("--install" in sys.argv or "-i" in sys.argv), default=None)
2024-04-17 18:14:17 -04:00
args = parser.parse_args()
2024-04-17 18:26:03 -04:00
f = sys.stdout
2024-04-17 18:14:17 -04:00
2024-04-17 18:26:03 -04:00
if args.install:
path = args.install
if not args.install.endswith("/"):
path += "/"
path += args.service_name
if not args.service_name.endswith(".service"):
path += ".service"
f = open(path, "wt")
def bprint(*args, **kwargs):
print(*args, file=f, **kwargs)
2024-04-17 18:14:17 -04:00
if __name__ == "__main__":
2024-05-01 20:10:09 -04:00
description = color_io.input_print("Please enter description")
exec_string = color_io.input_print("Please enter execution string")
2024-04-17 18:26:03 -04:00
bprint("[Unit]")
2024-05-01 20:10:09 -04:00
bprint("Description=" + description)
bprint()
bprint("[Service]")
bprint("Type=exec")
if args.working_dir is not None:
bprint("WorkingDirectory=" + args.working_dir)
if args.user is not None:
bprint("User=" + args.user)
if args.group is not None:
bprint("Group=" + args.group)
bprint("ExecStart=" + exec_string)
bprint("RestartSec=1s")
bprint("Restart=" + args.restart)
bprint("OOMPolicy=stop")
bprint()
bprint("[Install]")
bprint("WantedBy=" + args.wanted)