#! /usr/bin/env python3 import os import sys from string import Template from subprocess import check_call from subprocess import check_output EXEC = 'ground-control.py' SERVICE = 'ground-control.service' SYSTEM_DIR = '~/.config/systemd/user' CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) SERVICE_TEMPLATE = os.path.join(CURRENT_DIR, SERVICE) SERVICE_FILE = os.path.join(os.path.expanduser(SYSTEM_DIR), SERVICE) PYTHON = sys.executable EXEC_START = f'{PYTHON} {EXEC}' USER = check_output(['logname']).decode('utf-8').strip() with open(SERVICE_TEMPLATE) as f: serviceTemplate = Template(f.read()) serviceFile = serviceTemplate.substitute( workingDirectory=CURRENT_DIR, execStart=EXEC_START ) os.makedirs(os.path.dirname(SERVICE_FILE), exist_ok=True) with open(SERVICE_FILE, 'w') as f: f.write(serviceFile) check_call(['systemctl', '--user', 'daemon-reload']) check_call(['systemctl', '--user', 'enable', '--no-pager', SERVICE]) check_call(['systemctl', '--user', 'restart', '--no-pager', SERVICE]) check_call(['systemctl', '--user', 'status', '--no-pager', SERVICE])