0

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.

  • 2
    How do you run the script? – Kamil Maciorowski Apr 27 '20 at 21:36
  • Kamil, I have a service that I run from systemd which calls the pivoyager script. Dave – TripleD Apr 28 '20 at 04:02
  • Kamil, I am not sure the "How to use notify-send to notify users as root?" solves my problem. I am not root when running the script. Or am I? Dave – TripleD Apr 28 '20 at 04:03
  • From the answer there: "The command notify-send needs the environment variable DBUS_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:55
  • @Kamil, What user is running the service? Yes, in hindsight I do know this. It is user "pi". "pi" is the only user on the system. So, how do I set the environment variable DBUS_SESSION_BUS_ADDRESS, such that each time "pi" or better yet - any user should log in the variable is set? – TripleD Apr 28 '20 at 16:35
  • "pi is the only user on the system" – I bet there is root. 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 run notify-send as the right user, I think (sudo -u pi notify-send …?). – Kamil Maciorowski Apr 28 '20 at 16:48
  • @Kamil, yes, there is "root". My bad. – TripleD Apr 28 '20 at 21:08
  • @Kamil, this is the path statement from "/etc/profile". if [ "id -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:12
  • @kamil, so I would change the second path statement to read:

    PATH="/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:15
  • @kamil, or I would add the "PATH=/run/user/1000/bus" right after "PIVOYAGER=..." in my script? – TripleD Apr 28 '20 at 21:17
  • Your problem has nothing to do with PATH. Research how to set an environment variable in Linux. PATH is one variable and you need a separate DBUS_SESSION_BUS_ADDRESS variable. – Kamil Maciorowski Apr 28 '20 at 21:21
  • @Kamil, understand - makes sense as I re-read. Many, many thanks - I will research how to set an environment variable in Linux. Again, many thanks ! – TripleD Apr 29 '20 at 16:54

0 Answers0