Hello everyone. I’m trying to write a timer for systemd that runs a script I wrote. The script checks whether the battery is charged over 80% or under 40% and notifies the user using notify-send
.
According to journalctl --user -u bat_max
the problem seems to be that the service unit doesn’t find the executable script in my ~/.local/bin/
folder. I followed the advice in ArchWiki’s systemd/User article to make the PATH
environment variable available to systemd so that it can find the executable. Only difference is that instead of editing the ~/.bash_profile
file, I edited the ~/.bashrc
file which is where I set the PATH
env variable. When I run
systemctl --user show-environment
I obtain
...
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/<user>/.local/bin
...
So I’m not sure why it isn’t finding the executable. When I run the executable from the command line, it does so without any issues.
Any ideas about what could be going wrong are much appreciated.
Here’s the relevant output of journalctl -xb --user -u bat_max
.
Dec 26 02:03:46 systemd[991]: Starting Check if battery is over 80% charge and notify...
...
Dec 26 02:03:46 systemd[14286]: bat_max.service: Failed to locate executable check-battery-charge.sh: No such file or directory
░░ Subject: Process check-battery-charge.sh could not be executed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ The process check-battery-charge.sh could not be executed and failed.
░░
░░ The error number returned by this process is ERRNO.
Dec 26 02:03:46 systemd[14286]: bat_max.service: Failed at step EXEC spawning check-battery-charge.sh: No such file or directory
....
Dec 26 02:03:46 systemd[991]: bat_max.service: Main process exited, code=exited, status=203/EXEC
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ An ExecStart= process belonging to unit UNIT has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 203.
Dec 26 02:03:46 systemd[991]: bat_max.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ The unit UNIT has entered the 'failed' state with result 'exit-code'.
Dec 26 02:03:46 systemd[991]: Failed to start Check if battery is over 80% charge and notify.
░░ Subject: A start job for unit UNIT has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ A start job for unit UNIT has finished with a failure.
░░
░░ The job identifier is 7508 and the job result is failed.
Here are the timer, the service unit and the script in case it’s useful.
bat_max.timer
:
[Unit]
Description=Check if battery is over 80% charge and notify
[Timer]
AccuracySec=1sec
OnStartupSec=30sec
OnUnitActiveSec=1min
[Install]
WantedBy=timers.target
bat_max.service
:
[Unit]
Description=Check if battery is over 80% charge and notify
[Service]
RuntimeDirectory=battery-notification
RuntimeDirectoryPreserve=yes
Type=oneshot
ExecStart=check-battery-charge.sh
[Install]
WantedBy=bat_max.timer
check-battery-charge.sh
:
#!/bin/bash
currentCharge=`cat /sys/class/power_supply/BAT0/charge_now`
fullCharge=`cat /sys/class/power_supply/BAT0/charge_full`
tmpFile="battery-notification-sent"
if (( $(echo "$currentCharge/$fullCharge >= .8" | bc -l) )); then
if [ ! -e "$tmpFile" ]; then
notify-send --urgency=critical --icon=battery "Battery" \
"Hey, this is your battery talking: I'm charged above 80%"
paplay /usr/share/sounds/Oxygen-Sys-App-Error-Serious-Very.ogg
touch "$tmpFile"
fi
elif (( $(echo "$currentCharge/$fullCharge <= .4" | bc -l) )); then
if [ ! -e "$tmpFile" ]; then
notify-send --urgency=normal --icon=battery "Battery" \
"Hey, I'm charged below 40%"
paplay /usr/share/sounds/Oxygen-Sys-App-Error-Serious-Very.ogg
touch "$tmpFile"
fi
elif [ -e "$tmpFile" ]; then
rm "$tmpFile"
fi
Thanks for reading and having the intention to help me.