I cannot run tlp-stat from a systemd service script
by LiamMayfair from LinuxQuestions.org on (#4Y5MS)
Hi,
I've written a small shell script service on a systemd timer that polls the status of my laptop battery using TLP and then sends me a notification depending on how low it is.
The problem I'm having is that, when the script gets exec'd by systemd, I get the following error from tlp-stat:
Code:Jan 20 14:07:20 osiris.localdomain check-battery-level[29948]: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
Jan 20 14:07:20 osiris.localdomain sudo[29951]: pam_unix(sudo:auth): conversation failed
Jan 20 14:07:20 osiris.localdomain sudo[29951]: pam_unix(sudo:auth): auth could not identify password for [liam]The command in the script that throws the error specifically is
Code:currentCharge=$(sudo tlp-stat -b | rg Charge | rg -v Features | rg -o "[0-9]+.[0-9]" | head -1)I've tried removing the `sudo`, as the script should be run by systemd as root anyway (I don't specify any user in my unit file), but if I do that, tlp-stat also complains that I don't have root privileges to run that command (????). Any thoughts? Thank you! BTW, I'm using Fedora 30.
This is the script in full:
Code:#!/bin/env zsh
readonly CHARGE_LOW="20.0"
readonly CHARGE_WARN="10.0"
readonly CHARGE_CRIT="5.0"
readonly NOTIFICATION_TIMEOUT="10000"
readonly USERNAME=liam
readonly uid=$(id -u ${USERNAME})
echo "Querying current battery status..."
currentCharge=$(sudo tlp-stat -b | rg Charge | rg -v Features | rg -o "[0-9]+.[0-9]" | head -1)
currentStatus=$(cat /sys/class/power_supply/BAT0/status)
notificationBody="Battery is at ${currentCharge}%"
echo "Charge: ${currentCharge}. Status: ${currentStatus}"
if [[ "${currentStatus}" == "Discharging" ]]; then
case ${currentCharge} in
${CHARGE_LOW})
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${uid}/bus \
notify-send -u normal -a "BATTERY LOW" -t ${NOTIFICATION_TIMEOUT} -h string:bgcolor:#fff700 -h string:fgcolor:#000 ${notificationBody}
;;
${CHARGE_WARN})
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${uid}/bus \
notify-send -u critical -a "BATTERY WARNING" -t ${NOTIFICATION_TIMEOUT} -h string:bgcolor:#ffa200 -h string:fgcolor:#000 ${notificationBody}
;;
${CHARGE_CRIT})
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${uid}/bus \
notify-send -u critical -a "BATTERY CRITICAL" -t ${NOTIFICATION_TIMEOUT} -h string:bgcolor:#ff0000 -h string:fgcolor:#ffffff ${notificationBody}
;;
?)
exit 0
;;
esac
exit 0
fi
echo "Battery is charging, nothing to do"
exit 0And here are the systemd.service and systemd.timer.
Code:# /etc/systemd/system/check-battery-level.service
[Unit]
Description=Send low battery notifications
[Service]
Type=oneshot
ExecStart=/usr/local/bin/check-battery-levelCode:# /etc/systemd/system/check-battery-level.timer
[Unit]
Description=Send low battery notifications
[Timer]
OnBootSec=1m
OnUnitActiveSec=5
[Install]
WantedBy=timers.target


I've written a small shell script service on a systemd timer that polls the status of my laptop battery using TLP and then sends me a notification depending on how low it is.
The problem I'm having is that, when the script gets exec'd by systemd, I get the following error from tlp-stat:
Code:Jan 20 14:07:20 osiris.localdomain check-battery-level[29948]: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
Jan 20 14:07:20 osiris.localdomain sudo[29951]: pam_unix(sudo:auth): conversation failed
Jan 20 14:07:20 osiris.localdomain sudo[29951]: pam_unix(sudo:auth): auth could not identify password for [liam]The command in the script that throws the error specifically is
Code:currentCharge=$(sudo tlp-stat -b | rg Charge | rg -v Features | rg -o "[0-9]+.[0-9]" | head -1)I've tried removing the `sudo`, as the script should be run by systemd as root anyway (I don't specify any user in my unit file), but if I do that, tlp-stat also complains that I don't have root privileges to run that command (????). Any thoughts? Thank you! BTW, I'm using Fedora 30.
This is the script in full:
Code:#!/bin/env zsh
readonly CHARGE_LOW="20.0"
readonly CHARGE_WARN="10.0"
readonly CHARGE_CRIT="5.0"
readonly NOTIFICATION_TIMEOUT="10000"
readonly USERNAME=liam
readonly uid=$(id -u ${USERNAME})
echo "Querying current battery status..."
currentCharge=$(sudo tlp-stat -b | rg Charge | rg -v Features | rg -o "[0-9]+.[0-9]" | head -1)
currentStatus=$(cat /sys/class/power_supply/BAT0/status)
notificationBody="Battery is at ${currentCharge}%"
echo "Charge: ${currentCharge}. Status: ${currentStatus}"
if [[ "${currentStatus}" == "Discharging" ]]; then
case ${currentCharge} in
${CHARGE_LOW})
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${uid}/bus \
notify-send -u normal -a "BATTERY LOW" -t ${NOTIFICATION_TIMEOUT} -h string:bgcolor:#fff700 -h string:fgcolor:#000 ${notificationBody}
;;
${CHARGE_WARN})
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${uid}/bus \
notify-send -u critical -a "BATTERY WARNING" -t ${NOTIFICATION_TIMEOUT} -h string:bgcolor:#ffa200 -h string:fgcolor:#000 ${notificationBody}
;;
${CHARGE_CRIT})
DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${uid}/bus \
notify-send -u critical -a "BATTERY CRITICAL" -t ${NOTIFICATION_TIMEOUT} -h string:bgcolor:#ff0000 -h string:fgcolor:#ffffff ${notificationBody}
;;
?)
exit 0
;;
esac
exit 0
fi
echo "Battery is charging, nothing to do"
exit 0And here are the systemd.service and systemd.timer.
Code:# /etc/systemd/system/check-battery-level.service
[Unit]
Description=Send low battery notifications
[Service]
Type=oneshot
ExecStart=/usr/local/bin/check-battery-levelCode:# /etc/systemd/system/check-battery-level.timer
[Unit]
Description=Send low battery notifications
[Timer]
OnBootSec=1m
OnUnitActiveSec=5
[Install]
WantedBy=timers.target