I was making a battery notifier script for my raspberry pi3. The script is executing when I do
/usr/bin/python /home/pi/Documents/shutdown.py
and showing the popup notifications. However the service is not executing it or not showing the notification. I can see the python process if I do sudo systemctl status battery-notifier.service
.
battery-notifer.service
[Unit]
Description=Battery Notifier
[Service]
Type=simple
WorkingDirectory=/home/pi/Documents
ExecStart=/usr/bin/python /home/pi/Documents/shutdown.py
[Install]
WantedBy=multi-user.target
shutdown.py
import raspiupshat
import statistics
import subprocess
from time import sleep
raspiupshat.init()
while(True):
voltagesList = []
sleep(0.5)
currentVoltage = raspiupshat.getv()
voltagesList.append(currentVoltage)
medianVoltage = statistics.median(voltagesList)
if(medianVoltage>4):
subprocess.Popen(["notify-send","Battery Full!"])
This is the status of the service when I do sudo systemctl status battery-notifier.service
:
● battery-notifier.service - Battery Notifier
Loaded: loaded (/lib/systemd/system/battery-notifier.service; enabled)
Active: active (running) since Sat 2017-07-15 04:05:18 UTC; 48min ago
Main PID: 28384 (python)
CGroup: /system.slice/battery-notifier.service
└─28384 /usr/bin/python /home/pi/Documents/shutdown.py
Jul 15 04:05:18 raspberrypi systemd[1]: Started Battery Notifier.
subprocess.call("export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-goOEk5dZcK,guid=9c1f14175e6be0992b16e5155969b46c",shell=True)
,but it didn't work. – avistein Jul 15 '17 at 06:38dbus-send
) and each logged in user must then launch a program (for example in Python via using thedbus
package) which listens to that message to display something on the screen of their desktop? – akraf Jul 15 '17 at 12:44User=pi
in the service worked as a charm! Please edit your original answer with this so that it can help others too. – avistein Jul 15 '17 at 21:32User=
systemd parameter existed :-). So the credits would be undeserved. My idea was to develop two separate programs, the first at system level which deals with checking the battery status and the second started at user login which is notified by the first and displays the message. – akraf Jul 18 '17 at 21:57