From 2f1d0f8adc49ee2616e1b5294e24f51fc3763019 Mon Sep 17 00:00:00 2001 From: lenar Date: Thu, 2 Apr 2026 21:53:26 +0300 Subject: [PATCH] Self-daemonize tray app for proper error reporting on launch --- screenprofiler.py | 17 +++++++++++++++-- screenprofilercmd.sh | 13 ++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/screenprofiler.py b/screenprofiler.py index 0a52802..4785b80 100755 --- a/screenprofiler.py +++ b/screenprofiler.py @@ -277,7 +277,20 @@ def open_about_window(self): dialog.exec_() if __name__ == '__main__': + # Fork to background — parent exits, child continues as the tray app + pid = os.fork() + if pid > 0: + sys.exit(0) + + # Child: detach from terminal and run the tray app + os.setsid() + + # Redirect stdout/stderr so the detached process doesn't hold terminal fds + devnull = os.open(os.devnull, os.O_RDWR) + os.dup2(devnull, sys.stdout.fileno()) + os.dup2(devnull, sys.stderr.fileno()) + os.close(devnull) + app = QApplication(sys.argv) window = MainWindow() - exit_code = app.exec_() - sys.exit(exit_code) + sys.exit(app.exec_()) diff --git a/screenprofilercmd.sh b/screenprofilercmd.sh index 934a3ab..7540001 100755 --- a/screenprofilercmd.sh +++ b/screenprofilercmd.sh @@ -133,11 +133,14 @@ case $command in # Change to script directory to ensure correct working directory cd "$script_dir" || exit 1 - # Launch the Python tray application in the background - # Redirect output to /dev/null to avoid cluttering terminal - nohup python3 "$script_dir/screenprofiler.py" >/dev/null 2>&1 & - - print_success "Screen Profiler tray application launched" + # The Python script self-daemonizes: it forks, the parent exits + # with the appropriate exit code, and the child runs the tray app. + if python3 "$script_dir/screenprofiler.py"; then + print_success "Screen Profiler tray application launched" + else + print_error "Failed to launch tray application" + exit 1 + fi ;; # ------------------------------------------------------------------------