From 9b8f2fb8de9bd2bf1b13a84ab06e394aa9d7cb02 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 25 Jan 2022 02:30:02 +0300 Subject: app.py: don't do systemctl --user if no user instance --- app.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 67cdd6d..dd3edfc 100755 --- a/app.py +++ b/app.py @@ -311,7 +311,10 @@ class UserInstanceStatusTaskList(Task): self.tasks = {user.name: UserInstanceStatusTask.su(user) for user in systemd_users()} else: # As a regular user, we can only query ourselves. - self.tasks = {get_current_user().name: UserInstanceStatusTask()} + self.tasks = {} + user = get_current_user() + if user_instance_active(user): + self.tasks[user.name] = UserInstanceStatusTask() def run(self): for task in self.tasks.values(): @@ -363,6 +366,19 @@ def get_current_user(): return User(entry.pw_uid, entry.pw_name) +def user_instance_active(user): + # I'm pretty sure this is the way to determine if the user instance is + # running? + # Source: https://www.freedesktop.org/software/systemd/man/user@.service.html + unit_name = f'user@{user.uid}.service' + cmd = Systemctl.system('is-active', unit_name, '--quiet') + try: + cmd.run().result() + return True + except Exception: + return False + + # A pitiful attempt to find a list of possibly-systemd-enabled users follows # (i.e. users that might be running a per-user systemd instance). # I don't know of a better way than probing /run/user/UID. -- cgit v1.2.3