Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"...

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