def create_timer_service(service_name, args): timer = run_quiet("systemd-analyze calendar '{}'".format(args.timer)) if not bool(timer): print("Service type 'simple' (long running) since NOT using a timer.") return False next_run = timer.split("From now: ")[1].strip() accuracy_sec = timer_granularity(args.timer) print("Service type 'oneshot' since using a timer. Next run: {}".format( next_run)) service = (""" [Unit] Description=Running '{service_name}' on a schedule (generated by sysdm) Requires={service_name}.service [Timer] OnCalendar={timer} AccuracySec={accuracy_sec} [Install] WantedBy=timers.target """.replace("\n ", "\n").format(timer=args.timer, service_name=service_name, accuracy_sec=accuracy_sec).strip()) with open(os.path.join(args.systempath, "{}.timer".format(service_name)), "w") as f: f.write(service) return True
def create_service_template(args): here = os.path.abspath(".") fname, extra_args = args.fname_or_cmd.split()[0], " ".join( args.fname_or_cmd.split()[1:]) binary, cmd = get_cmd_from_filename(fname) service_name = fname + "_" + here.split("/")[-1] if binary else fname service_name = to_sn(service_name) fname = fname + " " # other binary if binary: fname = "" if args.notify_cmd != "-1": on_failure = "OnFailure={}-onfailure@%i.service".format( args.notify_cmd) else: on_failure = "" timer = run_quiet("systemd-analyze calendar '{}'".format(args.timer)) if bool(timer): service_type = "oneshot" restart = "" part_of = "" else: service_type = "simple" restart = "Restart=always\nRestartSec={delay}".format(delay=args.delay) part_of = "PartOf={service_name}_monitor.service".format( service_name=service_name) service = (""" [Unit] Description={service_name} service (generated by sysdm) After=network-online.target {part_of} {on_failure} [Service] {user_and_group} Type={service_type} {restart} ExecStart={cmd} {fname} {extra_args} WorkingDirectory={here} [Install] WantedBy=multi-user.target """.replace("\n ", "\n").format( service_name=service_name, cmd=cmd, fname=fname, extra_args=extra_args, here=here, restart=restart, part_of=part_of, on_failure=on_failure, service_type=service_type, user_and_group=user_and_group_if_sudo(args), ).strip()) return service_name, service
def delete(unit, systempath): service_name = unit.replace(".", "_") path = systempath + "/" + service_name for s in [ service_name, service_name + "_monitor", service_name + ".timer" ]: if is_unit_enabled(s): _ = systemctl("disable {}".format(s)) print("Disabled unit {}".format(s)) else: print("Unit {} was not enabled so no need to disable it".format(s)) if is_unit_running(s): _ = systemctl("stop {}".format(s)) print("Stopped unit {}".format(s)) else: print("Unit {} was not started so no need to stop it".format(s)) _ = systemctl("daemon-reload") o = run_quiet("rm {}".format(path + ".service")) o = run_quiet("rm {}".format(path + "_monitor.service")) o = run_quiet("rm {}".format(path + ".timer")) print("Deleted {}".format(path + ".service")) print("Deleted {}".format(path + "_monitor.service")) print("Deleted {}".format(path + ".timer")) print("Delete Succeeded!")
def create_service_template(fname_or_cmd, notify_cmd, timer, delay, root, killaftertimeout): here = os.path.abspath(".") fname, extra_args = fname_or_cmd.split()[0], " ".join( fname_or_cmd.split()[1:]) binary, cmd = get_cmd_from_filename(fname) service_name = fname + "_" + here.split("/")[-1] if binary else fname service_name = to_sn(service_name) fname = fname + " " # other binary if binary: fname = "" if notify_cmd != "-1": on_failure = "OnFailure={}-onfailure@%i.service".format(notify_cmd) else: on_failure = "" if timer is not None: timer = run_quiet("systemd-analyze calendar '{}'".format(timer)) if bool(timer): service_type = "oneshot" restart = "" part_of = "" else: service_type = "simple" restart = "Restart=always\nRestartSec={delay}".format(delay=delay) part_of = "PartOf={service_name}_monitor.service".format( service_name=service_name) user_and_group = user_and_group_if_sudo(root) if timer: install = "" else: wanted_by = "multi-user.target" if user_and_group.strip( ) else "default.target" wanted_by += " " + service_name + "_monitor.service" install = "[Install]\nWantedBy={wanted_by}".format(wanted_by=wanted_by) service = (""" [Unit] Description={service_name} service (generated by sysdm) After=network-online.target {part_of} {on_failure} [Service] {user_and_group} Type={service_type} {restart} TimeoutStopSec={killaftertimeout} ExecStart={cmd} {fname} {extra_args} WorkingDirectory={here} {install} """.replace("\n ", "\n").format( service_name=service_name, cmd=cmd, fname=fname, extra_args=extra_args, here=here, restart=restart, part_of=part_of, on_failure=on_failure, service_type=service_type, user_and_group=user_and_group, install=install, killaftertimeout=killaftertimeout, ).strip()) return service_name, service
def create_service_template(fname_or_cmd, notifier, timer, delay, root, killaftertimeout, restart, workdir: str = "", env_vars=[]): here = workdir or os.path.abspath(".") fname, extra_args = fname_or_cmd.split()[0], " ".join( fname_or_cmd.split()[1:]) binary, cmd = get_cmd_from_filename(fname) service_name = get_service_name(fname_or_cmd) + "_" + here.split( "/")[-1] if binary else fname service_name = to_sn(service_name) fname = fname + " " start_info = "" # other binary if binary: fname = "" if notifier is not None: on_failure = "OnFailure={}-onfailure@%i.service".format(notifier) else: on_failure = "" if timer is not None: timer = run_quiet("systemd-analyze calendar '{}'".format(timer)) if bool(timer): service_type = "oneshot" restart = "" part_of = "" else: service_type = "simple" start_info = "StartLimitBurst=2\nStartLimitIntervalSec=15s" if restart else "" restart = "Restart=always\nRestartSec={delay}".format( delay=delay) if restart else "" part_of = "PartOf={service_name}_monitor.service".format( service_name=service_name) user_and_group = user_and_group_if_sudo(root) if timer: install = "" else: wanted_by = "multi-user.target" if user_and_group.strip( ) else "default.target" wanted_by += " " + service_name + "_monitor.service" install = "[Install]\nWantedBy={wanted_by}".format(wanted_by=wanted_by) env_var_section = "\n".join([ f'Environment={x if "=" in x else x + "=" + os.environ[x]}' for x in env_vars ]) service = (""" [Unit] Description={service_name} service (generated by sysdm) After=network-online.target {part_of} {on_failure} {start_info} [Service] {user_and_group} Type={service_type} {restart} TimeoutStopSec={killaftertimeout} ExecStart={cmd} {fname} {extra_args} WorkingDirectory={here} Environment=PYTHONUNBUFFERED=1 {env_var_section} {install} """.replace("\n ", "\n").format(**locals()).strip()) return service_name, service