Skip to content

Instantly share code, notes, and snippets.

@rsyring
Created May 30, 2025 20:12
Show Gist options
  • Save rsyring/3a046d818bc9154a2c6f619d369427a1 to your computer and use it in GitHub Desktop.
Save rsyring/3a046d818bc9154a2c6f619d369427a1 to your computer and use it in GitHub Desktop.
Systemd Run for Mise Python Tasks
import contextlib
class SystemdRun:
def __init__(self, unit_name: str, restart_wait: str = '5s'):
self.unit_name = unit_name
self.restart_wait = restart_wait
def stop(self):
sub_run('systemctl', '--user', 'stop', self.unit_name, capture=True, check=False)
def mise_exec(self, *exec_args: list[str]):
self.stop()
sub_run(
'systemd-run',
'--user',
'--unit',
self.unit_name,
'--working-directory',
environ['MISE_PROJECT_ROOT'],
'--property=Restart=on-failure',
f'--property=RestartSec={self.restart_wait}',
'mise',
'exec',
'--',
*exec_args,
)
try:
sub_run(
'journalctl',
'--user',
'--follow',
'--since',
'now',
'--unit',
self.unit_name,
# --output=cat gives us color codes from the journal so the output for the dev looks
# like what it would have if they had seen it directly from the process output.
'--output=cat',
)
except KeyboardInterrupt:
# For some reason, the stop process likes to throw a TERM signal too in this case.
with contextlib.suppress(KeyboardInterrupt):
self.stop()
from tasks_lib import SystemdRun
if __name__ == '__main__':
SystemdRun('poster-web').mise_exec('poster', 'run')
@rsyring
Copy link
Author

rsyring commented May 30, 2025

In order to run multiple units at the same time, the code should be adjusted so the log output is an explicit call at the end. So, usage would look more like:

sdr = SystemdRun('poster')
sdr.mise_exec('poster', 'run')
sdr.mise_exec('celery', ...)
sdr.mise_exec('vite', 'run')
sdr.logs()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment