From 7efe01f8f2fef13fcb398cc2a7b2f290b3b821f5 Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Wed, 12 Apr 2023 12:19:57 +0200 Subject: [PATCH] add function for desktop notification --- jobhelper.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/jobhelper.sh b/jobhelper.sh index d413db4..02271ef 100755 --- a/jobhelper.sh +++ b/jobhelper.sh @@ -82,6 +82,11 @@ function j_init(){ j_requireBinary "tee" j_requireBinary "touch" + # for notify-send in j_notify() + if [ -n "$SUDO_USER" ]; then + export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u $SUDO_USER)/bus + fi + # j_read } @@ -419,6 +424,30 @@ function _getFqdn(){ # echo "INFO: FQDN is [$FQDN]" } +# show a desktop notification using notify-send +# param string summary (aka title) +# param string message text +# paran integer optional: exitcode; if set it adds a prefix OK or ERRROR on summary and sets urgency on error +function j_notify(){ + local _summary="IML BACKUP :: $1" + local _body="$2" + local _rc="$3" + + local _urgency="normal" + + if [ -n "$DBUS_SESSION_BUS_ADDRESS" ]; then + if [ -n "$_rc" ]; then + if [ "$_rc" = "0" ]; then + _summary="OK: ${_summary}" + else + _summary="ERROR: ${_summary}" + _urgency="critical" + fi + fi + su "$SUDO_USER" -c "notify-send --urgency=${_urgency} '${_summary}' '${_body}'" + fi +} + # ------------------------------------------------------------ # check if it was startet with a given user # This is skipped if MS windows was detected with "mingw". -- GitLab