Good Day, I am new to Linux (Raspberry) and am trying to get notify-send to work from a script (see below).
Notify send works from the terminal CLI and displays on my GUI. But it does not work from the script. I am using Raspberry Buster - with all updates.
#!/bin/bash
# This should point to the pivoyager executable
PIVOYAGER=/usr/local/bin/pivoyager
while true;
do
# Phase 1: Check every 5 seconds if we are USB powered.
# If we are not on USB power, go to phase 2.
while $PIVOYAGER status flags | grep "pg";
do
sleep 5
done
# Phase 2: Do something before shutdown. (optional)
# e.g. send an email, warn users, etc.
#notify-send "We are on Battery Power. Save all your work."
sleep 30
# Just check if power was miraculously restored.
if ! $PIVOYAGER status flags | grep "pg"; then
break
else
notify-send "Power was restored. Shutdown aborted!"
fi
done
# Phase 3: Initiate the shutdown.
# - Enable automatic power on once USB power is restored.
# - Program full power cut in 25 seconds.
$PIVOYAGER enable power-wakeup
$PIVOYAGER watchdog 25
sudo shutdown now
Thanks in advance for your help.
notify-send
needs the environment variableDBUS_SESSION_BUS_ADDRESS
set and must be called with the owner of this session bus as user." If the script runs from a systemd service then these conditions are not automatically met. I don't know as what user the service runs; you should know. – Kamil Maciorowski Apr 28 '20 at 04:55pi
is the only user on the system" – I bet there isroot
. Well, if the variable, when it works, is always the same (unix:path=/run/user/1000/bus
?), add it to/etc/profile
or hardcode in the script. You will still need to runnotify-send
as the right user, I think (sudo -u pi notify-send …
?). – Kamil Maciorowski Apr 28 '20 at 16:48id -u
" -eq 0 ]; then PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" else PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games" fi – TripleD Apr 28 '20 at 21:12PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/run/user/1000/bus" correct? and the reboot to test.
– TripleD Apr 28 '20 at 21:15PATH
. Research how to set an environment variable in Linux.PATH
is one variable and you need a separateDBUS_SESSION_BUS_ADDRESS
variable. – Kamil Maciorowski Apr 28 '20 at 21:21