2023-10-30 09:20:48 -04:00
|
|
|
#! /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'
|
2024-04-04 09:43:44 -04:00
|
|
|
SYSTEM_DIR = '~/.config/systemd/user'
|
2023-10-30 09:20:48 -04:00
|
|
|
|
|
|
|
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
SERVICE_TEMPLATE = os.path.join(CURRENT_DIR, SERVICE)
|
2024-04-04 09:43:44 -04:00
|
|
|
SERVICE_FILE = os.path.join(os.path.expanduser(SYSTEM_DIR), SERVICE)
|
2023-10-30 09:20:48 -04:00
|
|
|
PYTHON = sys.executable
|
|
|
|
EXEC_START = f'{PYTHON} {EXEC}'
|
|
|
|
USER = check_output(['logname']).decode('utf-8').strip()
|
2024-04-04 10:32:05 -04:00
|
|
|
LINGER = check_output(['loginctl', 'show-user', USER, '--property=Linger']).decode('utf-8').strip()
|
2023-10-30 09:20:48 -04:00
|
|
|
|
|
|
|
with open(SERVICE_TEMPLATE) as f:
|
|
|
|
serviceTemplate = Template(f.read())
|
|
|
|
|
|
|
|
serviceFile = serviceTemplate.substitute(
|
|
|
|
workingDirectory=CURRENT_DIR,
|
2024-04-04 09:43:44 -04:00
|
|
|
execStart=EXEC_START
|
2023-10-30 09:20:48 -04:00
|
|
|
)
|
|
|
|
|
2024-04-04 09:43:44 -04:00
|
|
|
os.makedirs(os.path.dirname(SERVICE_FILE), exist_ok=True)
|
|
|
|
|
2023-10-30 09:20:48 -04:00
|
|
|
with open(SERVICE_FILE, 'w') as f:
|
|
|
|
f.write(serviceFile)
|
|
|
|
|
2024-04-04 09:43:44 -04:00
|
|
|
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])
|
2024-04-04 10:32:05 -04:00
|
|
|
|
|
|
|
if LINGER != 'Linger=yes':
|
|
|
|
print(f'\n>>> Please run `sudo loginctl enable-linger {USER}` to allow this service to run without being logged in.')
|