Created
May 30, 2025 20:12
-
-
Save rsyring/3a046d818bc9154a2c6f619d369427a1 to your computer and use it in GitHub Desktop.
Systemd Run for Mise Python Tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tasks_lib import SystemdRun | |
if __name__ == '__main__': | |
SystemdRun('poster-web').mise_exec('poster', 'run') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: