diff --git a/deploy/scripts/libexec/left4me-journalctl b/deploy/scripts/libexec/left4me-journalctl index 2e5d3df..f3921b6 100755 --- a/deploy/scripts/libexec/left4me-journalctl +++ b/deploy/scripts/libexec/left4me-journalctl @@ -37,6 +37,16 @@ case "$follow_flag" in esac unit="left4me-server@${name}.service" + +if [ -x /bin/systemctl ]; then + systemctl=/bin/systemctl +elif [ -x /usr/bin/systemctl ]; then + systemctl=/usr/bin/systemctl +else + printf '%s\n' 'systemctl not found at /bin/systemctl or /usr/bin/systemctl' >&2 + exit 69 +fi + if [ -x /bin/journalctl ]; then journalctl=/bin/journalctl elif [ -x /usr/bin/journalctl ]; then @@ -46,8 +56,20 @@ else exit 69 fi +# Anchor `--since` to the moment systemd began the unit's current start +# transaction so the log panel starts at the latest run. Force LC_ALL=C so +# the day-of-week prefix is in a locale journalctl reliably parses. +start_time=$(LC_ALL=C "$systemctl" show -p InactiveExitTimestamp --value "$unit" 2>/dev/null || true) + +if [ -n "$start_time" ]; then + if [ -n "$follow_arg" ]; then + exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat "$follow_arg" + fi + exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat +fi + +# Unit has never run: no --since cutoff. `-f` will attach on first start. if [ -n "$follow_arg" ]; then exec "$journalctl" -u "$unit" -n "$lines" -o cat "$follow_arg" fi - exec "$journalctl" -u "$unit" -n "$lines" -o cat diff --git a/deploy/scripts/tests/test_journalctl_helper.py b/deploy/scripts/tests/test_journalctl_helper.py index 8566518..c04f94b 100644 --- a/deploy/scripts/tests/test_journalctl_helper.py +++ b/deploy/scripts/tests/test_journalctl_helper.py @@ -22,10 +22,17 @@ def test_journalctl_helper_passes_shell_syntax_check_and_rejects_bad_args(tmp_pa env=env_with_fake_commands(tmp_path), check=False, ) - assert result.returncode != 0 - assert not marker.exists() + assert result.returncode != 0, f"helper accepted bad args: {args!r}" + assert not marker.exists(), f"helper invoked journalctl for: {args!r}" script = JOURNALCTL_HELPER.read_text() assert 'unit="left4me-server@${name}.service"' in script + # Anchors `--since` to the unit's most recent start so the panel shows + # the current run (and any post-restart lines until reload). + assert 'InactiveExitTimestamp' in script + assert 'LC_ALL=C' in script + assert 'exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat "$follow_arg"' in script + assert 'exec "$journalctl" -u "$unit" --since "$start_time" -n "$lines" -o cat' in script + # Never-started fallback keeps the legacy unit-only form. assert 'exec "$journalctl" -u "$unit" -n "$lines" -o cat "$follow_arg"' in script assert 'exec "$journalctl" -u "$unit" -n "$lines" -o cat' in script