[HowTo] Create auto desktop-notification of error message from journalctl

Currently you do not notice when an error message appears in the log dmesg or journalctl,
Auto desktop-notification of error log might help to make you notice easily.

Installation

journalctl-desktop-notification-git is available in AUR

Configuration is located in /etc/journalctl-desktop-notification.conf

  • Exclusive annoying spam errors.
    ERROR_FILTER="Spam1|Spam2|Spam3"

  • Time frequency (secound) of the check journalctl
    TIME_INTERVAL="2"

  • Custom terminal app will be opened.
    Example: konsole for KDE terminal
    TERMINAL="konsole"

  • Custom icon of error message.
    ERROR_ICON="system-error"

  • Custom log level: Warning level is 4, error level is 3, critical level is 2.
    LOG_LEVEL="3"

How to enable it for autostart:

cp /usr/share/applications/journalctl-desktop-notification.desktop ~/.config/autostart/

OR


Manual without installing AUR package

  1. Install dunst that is for desktop-notification provided by most desktop environments

  2. Create a new script /usr/local/bin/journal-desktop-notify

#!/bin/env bash

get_timestamp_from_log()
{ 
    timestamp=$(journalctl --output=short-unix -p 3 -n 1 | grep -m1 '' | cut -d ' ' -f 1)
}

sleep 3

get_timestamp_from_log
OLD_TIMESTAMP_LINE=$timestamp

while sleep 2; do
    get_timestamp_from_log
    TIMESTAMP_LINE=$timestamp

    if [ -n "$TIMESTAMP_LINE" ] && [ "$TIMESTAMP_LINE" != "$OLD_TIMESTAMP_LINE" ]; then
        MESSAGE=$(journalctl --output=cat -p 3 -n 1)
        ACTION=$(dunstify --appname="Detected the error!" --icon=system-error --action="default,Reply" --action="openAction,Open" --action="disableAction,Disable" --action="closeAction,Close" "Please check journalctl!" "$MESSAGE")
        if [ "disableAction" == "$ACTION" ]; then
            echo "Disabled"
            break
        elif [ "closeAction" == "$ACTION" ]; then
            echo "Closed"
        elif [ -x "$(which gnome-terminal)" ]; then
            # Terminal will be opened when clicking the notification
            case "$ACTION" in
            "default")
                gnome-terminal -e "journalctl --no-page -p err -b -f"
                ;;
            "openAction")
                gnome-terminal -e "journalctl --no-page -p err -b -f"
                ;;
            esac
        elif [ -x "$(which konsole)" ]; then
            case "$ACTION" in
            "default")
                konsole -e "journalctl --no-page -p err -b -f"
                ;;
            "openAction")
                konsole -e "journalctl --no-page -p err -b -f"
                ;;
            esac
        elif [ -x "$(which xterm)" ]; then
            case "$ACTION" in
            "default")
                xterm -e "journalctl --no-page -p err -b -f"
                ;;
            "openAction")
                xterm -e "journalctl --no-page -p err -b -f"
                ;;
            esac
        fi

        get_timestamp_from_log
        OLD_TIMESTAMP_LINE=$timestamp
    fi
done
  1. Make this script executable:
sudo chmod +x /usr/local/bin/journal-desktop-notify
  1. Create a new script as Desktop app: ~/.local/share/applications/journal-notify.desktop
[Desktop Entry]
Name=Journal Notify
Comment=Auto notification of journalctl
Icon=system-error
Exec=/usr/local/bin/journal-desktop-notify
Terminal=false
Type=Application
Categories=Utility
  1. Create a new symlink for startup:
ln -s /home/$USER/.local/share/applications/journal-notify.desktop ~/.config/autostart/
  1. Reboot

How to test:

echo 'BTRFS error test' | systemd-cat -p err

KDE:
image

Gnome:
image


Notification appears when which error messages are excluded or included

How to exclude error messages like spams:

Just add grep -iEv 'spam1|spam2' behind journalctl in this script /usr/local/bin/journal-desktop-notify

Example:
Change

get_timestamp_from_log()
{ 
    timestamp=$(journalctl --output=short-unix -p 3 -n 1 | grep -m1 '' | cut -d ' ' -f 1)
}

to

get_timestamp_from_log()
{ 
    timestamp=$(journalctl --output=short-unix -p 3 -n 1 | grep -m1 '' | grep -iEv 'dolphin|systemd-coredump' | cut -d ' ' -f 1)
}

How to include some error messages only:

Just add grep -iE 'keyword1|keyword2' behind journalctl in the function get_timestamp_from_log()
Example:

timestamp=$(journalctl --output=short-unix -p 3 -n 1 | grep -m1 '' | grep -iE 'BTRFS' | cut -d ' ' -f 1)

How to remove timeout of notification

Notification disappears after the default timeout, but you might miss it when you are not there or distracted somewhere else.

If you do not want timeout of notification, then adding the option -u "critical" in the script:
Change

ACTION=$(dunstify --appname="Detected the error!" --icon=system-error --action="default,Reply" --action="openAction,Open" --action="closeAction,Close" "Please check journalctl!" "$MESSAGE" )

to

ACTION=$(dunstify -u "critical" --appname="Detected the error!" --icon=system-error --action="default,Reply" --action="openAction,Open" --action="closeAction,Close" "Please check journalctl!" "$MESSAGE" )

Troubleshooting

KDE Plasma notification is automatically replaced with “knopwob dunst” notification after some Manjaro update.

If you want to switch back KDE default notification, just reinstall knotifications and knotifyconfig to reset the config of KDE notification, then reboot.
If it does not work, try to do the other solution:

11 Likes

I changed the category as you’re not asking for support.

Will try that, thanks for sharing.

1 Like

ShellCheck: SC2236 – Use `-n` instead of `! -z`. – Use -n instead of ! -z.

Added support for gnome-terminal or xterm:

        # Terminal will be opened when clicking the notification
        if [ -x "$(which gnome-terminal)" ]; then
            case "$ACTION" in
            "default")
                gnome-terminal -e "journalctl --no-page -p err -b -f"
                ;;
            "openAction")
                gnome-terminal -e "journalctl --no-page -p err -b -f"
                ;;
            esac
        elif [ -x "$(which xterm)" ]; then
            case "$ACTION" in
            "default")
                xterm -e "journalctl --no-page -p err -b -f"
                ;;
            "openAction")
                xterm -e "journalctl --no-page -p err -b -f"
                ;;
            esac
        elif [ -x "$(which konsole)" ]; then
            case "$ACTION" in
            "default")
                konsole -e "journalctl --no-page -p err -b -f"
                ;;
            "openAction")
                konsole -e "journalctl --no-page -p err -b -f"
                ;;
            esac
        fi
2 Likes

Update:

  • Disable timeout of notification if you do not want timeout. There is here: :point_down:

Nice script, but is there a reason for using dunstify?
Why not notify-send? (ArchWiki: Desktop notifications - Bash)
It should be already installed…

Edit: You are absolutly right…played a little around with notify-send/gdbus and dunstify is way better…
I really appreciate your script :slight_smile:

I tried, but as far as I found libnotify does not support buttons in notifications. dunstify uses libnotify, anyway.

1 Like

Update

  • Created a new Desktop app “Journal Notify” in app menu.
  • The notification added a new button “Disable” that will stop the automatic notification until reboot or clicking this notify app.

Update

That wasn’t working, but just reinstalling the KDE notification packages made them default again.

sudo pacman -S knotifications knotifyconfig

//EDIT: and it doesn’t survive multiple reboots, it gets back to defaulting to this ugly notifications. not sure how to force it to stay on KDE’s default notification.
//EDIT2: seems like proper fix https://unix.stackexchange.com/questions/603186/wrong-notification-system-in-kde-plasma

mkdir -p ~/.local/share/dbus-1/services/
ln -s /usr/share/dbus-1/services/org.kde.plasma.Notifications.service \
  ~/.local/share/dbus-1/services/org.kde.plasma.Notifications.service

Also, the KDE case needs to be placed before the Xtrem case, or else if you have Xterm installed on your KDE installation, this is what is picked first instead of Konsole.

Update

I created an AUR package journalctl-desktop-notification-git

You can install this without manual setup.

Configuration is located in /etc/journalctl-desktop-notification.conf

  • Exclusive annoying spam errors
    ERROR_FILTER="Spam1|Spam2|Spam3"

  • Time frequency (secound) of the check journalctl
    TIME_INTERVAL="2"

  • Custom terminal app will be opened Example: konsole for KDE terminal
    TERMINAL="konsole"

  • Custom icon of error message
    ERROR_ICON="system-error"

How to enable it for autostart:

cp /usr/share/applications/journalctl-desktop-notification.desktop ~/.config/autostart/
3 Likes
makedepends=('git' 'dunst' 'systemd')

# should be
depends=('bash' 'dunst' 'systemd')
makedepends=('git')
cp /user/local/share/applications/journalctl-desktop-notification.desktop ~/.config/autostart/

# should be
cp /usr/share/applications/journalctl-desktop-notification.desktop ~/.config/autostart/

And probably include the troubleshooting in README.md

1 Like

There’s a bit more improvements that should be made as well. See VCS package guidelines.

# Maintainer: Zesko
pkgname=journalctl-desktop-notification-git
pkgver=r6.1d5b608
pkgrel=1
pkgdesc="The notification notifies on the desktop when any error message appeared in Journalctl log."
arch=('any')
url="https://gitlab.com/Zesko/journalctl-desktop-notification"
license=('GPL3')
depends=('dunst' 'systemd')
makedepends=('git')
provides=("${pkgname%-git}")
conflicts=("${pkgname%-git}")
backup=("etc/${pkgname%-git}.conf")
source=('git+https://gitlab.com/Zesko/journalctl-desktop-notification.git')
sha256sums=('SKIP')

pkgver() {
  cd "$srcdir/${pkgname%-git}"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

package() {
  cd "$srcdir/${pkgname%-git}"
  cp -vr usr etc "$pkgdir"
}
2 Likes

It is fixed now.

@stasadev
I was lazy with testing, thanks for the report.

@Yochanan
Thanks for your help!

Update

A small change for the AUR package: journalctl-desktop-notification-git

The config file /etc/journalctl-desktop-notification.conf added a new option: LOG_LEVEL

The default log level is 3.


I created a new AUR package btrfs-desktop-notification-git that monitors dmesg for Btrfs filesystem only. Its default log level is 4 (Warning level) of dmesg.

2 Likes

Hi,
I have been using your script since you first introduced it and I like it very much.
Only now I stumbled over the fact that it is a bash script and should actually be named with a trailing “.sh”.
Although, actually it does not matter :wink: