Skip to content

Instantly share code, notes, and snippets.

@chriskuehl
Last active March 4, 2024 20:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriskuehl/9ab4c74c19f2f4cb883744171335ac44 to your computer and use it in GitHub Desktop.
Save chriskuehl/9ab4c74c19f2f4cb883744171335ac44 to your computer and use it in GitHub Desktop.
Fixing Ctrl-C in `less` when using systemd's `journalctl`

journalctl is the systemd tool for viewing and tailing system logs. It's cool but has one infuriating bug: when you hit ^C, it kills both journalctl and your pager. less has lots of useful behavior based on ^C (such as to stop tailing a log), so this makes journalctl practically useless in my view :)

It sets the K option on less which has this effect, but then it also sends SIGTERM itself when journalctl receives SIGINT, so unsetting that K is not effective, either.

Enter dumb-init

You can use dumb-init to rewrite SIGINT to another signal, like SIGURG (which by default does nothing) on its way to journalctl, and then to rewrite SIGURG back to SIGINT before going to the pager.

root@whirlwind:~#  ls -l /usr/local/bin/{dumb-init,dumb-pager,journalctl}
-rwxr-xr-x 1 root root 46400 Oct 10 14:19 /usr/local/bin/dumb-init
-rwxr-xr-x 1 root root    98 Nov 24 18:58 /usr/local/bin/dumb-pager
-rwxr-xr-x 1 root root   101 Nov 24 18:58 /usr/local/bin/journalctl

root@whirlwind:~# cat /usr/local/bin/journalctl
#!/bin/bash -eu
export PAGER=/usr/local/bin/dumb-pager
exec ./dumb-init --rewrite 2:23 -- /bin/journalctl $@

root@whirlwind:~# cat /usr/local/bin/dumb-pager
#!/bin/bash -eu
export LESS=$(sed 's/K//' <<< "$LESS")
exec ./dumb-init --rewrite 23:2 -- less $@

Now you can use journalctl as usual, and it doesn't get in less's way.

File this one under "unintended (ab)uses of dumb-init"...

@ewaldbenes
Copy link

Not sure if it's a bug or just an unexpected design decision for us. At least you can fix it even more easily.

journalctl exposes the environment variable SYSTEMD_LESS which lets you overwrite how it calls less under the hood. Setting SYSTEMD_LESS=FRSXM makes less behave as "normal".

If you do not want to set it on each call you can write export SYSTEMD_LESS=FRSXM into your .bashrc, or .zshrc file. Adapt to the shell you use!

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