Audio sound and notification of low battery using crontab SOLVED

steven1976

New Member
Joined
Apr 17, 2026
Messages
5
Reaction score
2
Credits
68
Some years ago, I found a similar script online and modified it to run on my Linux mint system to get various star trek alerts (shows you how old I am) based on my battery state.
I am not a programmer, just a retired engineer with time on his hands.
See attached script. and systemctl files. I uploaded them as text.
After mint version upgrades, and possibly lenovo idea pad updates the crontab execution of the script no longer seems to be working,
The script is executable.
The script runs in terminal or just run
The crontab entry is:
* * * * * /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1

Have run systemctl status cron, and it seems the cron job is running, but the script itself is not executing properly.

I have looked around and tried various things and cannot find where i have gone wrong. Shell, environment, path ??? Adds to crontab or script file itself ???

Any suggestions would be appreciated


Steve1976
 
Last edited:


Some years ago, I found a similar script online and modified it to run on my Linux mint system to get various star trek alerts (shows you how old I am) based on my battery state.
I am not a programmer, just a retired engineer with time on his hands.
See attached script. and systemctl files. I uploaded them as text.
After mint version upgrades, and possibly lenovo idea pad updates the crontab execution of the script no longer seems to be working,
The script is executable.
The script runs in terminal or just run
The crontab entry is:
* * * * * /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1

Have run systemctl status cron, and it seems the cron job is running, but the script itself is not executing properly.

I have looked around and tried various things and cannot find where i have gone wrong. Shell, environment, path ??? Adds to crontab or script file itself ???

Any suggestions would be appreciated


Steve1976
The first thing to check is whether the script will run on the machine, so it's worth just running it to see if it actually works. I can't test it here on a desktop with no battery. If the script doesn't work, of course cron couldn't run it. If it does work, but fails to work on cron, then the cron entry is likely to be insufficient.

Alternative cron entries for the executable one could try are:
Code:
/bin/bash /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1
or
Code:
/bin/bash -c /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1

Alternatively, set up a systemd timer file with it's corresponding unit file. That will work if the script works.
 
Last edited:
Looking at your alert.sh.txt file I see SOUND_COMMAND="${SOUND_COMMAND:-/usr/bin/play}"
does the play file exist /usr/bin/play ?
There is a play link to the executable inside /usr/bin.
I think this was placed here by the SOX install

Since the script plays sounds when I double click and then run in terminal, I think its getting to the play command ok, at least when I run it manually.

crontab seems to have path issues because its default path is not the same as what the user has.
I have tried varous edits suggested on the web to force crontab to look in the user's path, but cannot find something that works.

I will also try the changes suggesed by OSPREY

thanks.
 
The first thing to check is whether the script will run on the machine, so it's worth just running it to see if it actually works. I can't test it here on a desktop with no battery. If the script doesn't work, of course cron couldn't run it. If it does work, but fails to work on cron, then the cron entry is likely to be insufficient.

Alternative cron entries for the executable one could try are:
Code:
/bin/bash /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1
or
Code:
/bin/bash -c /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1

Alternatively, set up a systemd timer file with it's corresponding unit file. That will work if the script works.
1. When I run the script in terminal, it appears to work. So it is getting to the files and play command in my environment path.
I have read crontab default path is not the same file as the users path.
I previously tried changes to the both crontab file and the script itself to modify path.
I could not find anything that worked.

2. I assume those cron commands have to be prefixed by * * * * * so as to run every minute. (?)
Let me know if the crontab entry should look different than below
- Changed crontab (not the script itself) to :
* * * * * /bin/bash /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1
This made no change

- Next tried changing crontab (not the script itself) to :
* * * * * /bin/bash -c /home/steven/Downloads/battery/alert.sh >/dev/null 2>&1
That produced no results either.

Unless you have any other ideas, Ill take a look at how to set up systemd timer.

Thanks
 
Unless you have any other ideas, Ill take a look at how to set up systemd timer.

Thanks
The only other thing that comes to mind at the moment is when writing the crontab, using the crontab -e editing process, one can place the variable: SHELL=/bin/bash, at the very top of the file which will instruct cron to use the bash shell. In that case, one would remove "/bin/bash" from the execution lines in post #2, and use the full paths to the executables as shown in post #1.

If that fails, systemd timers would be a smart alternative. There's a reasonable exposition on them here: https://wiki.archlinux.org/title/Systemd/Timers
 
FYI. (I am an engineer, not a programmer so take this with no warranty)

After spending days trying to figure this out, I think Linux Mint 22.1 update somehow introduced a 6-8 minute delay in valid execution of the alert.sh script. Cron runs, but the alert.sh doesn't execute successfully for up to 6-8 minutes after boot. Don't know why.

You can imagine the confusion, as I tried various things, when (after the 6-8 minute delay) it started to work. Not because of the fix i was trying, but from the delay !!!
As I tried various things, I kept thinking, aha, that fixed it. Reboot, and then the fix didn't work.

crontab -e
PATH=/usr/bin:/bin:/home/steven/.local/bin/
*/2 * * * * /home/steven/cronjobs/alert.sh >/dev/null 2>&1 (run every 2 minutes)
@reboot sleep 30 && /usr/local/bin/restart_cron.sh (Not sure this is necessary)

22.1 also replaced PulseAudio with Pipewire: (may not have been the problem but i reinstalled pulseaudio anyway)

Also , somewhere along the line, acpi -b was giving me different results than a few years ago, but this may have been a Lenov Ideapad update. So that was a separate problem I had to fix in the grep logic in the script
 
Last edited:
Depending on which distro you run...

ls /usr/share/sounds/freedesktop/stereo/
alarm-clock-elapsed.oga audio-channel-side-left.oga device-added.oga network-connectivity-established.oga screen-capture.oga
audio-channel-front-center.oga audio-channel-side-right.oga device-removed.oga network-connectivity-lost.oga service-login.oga
audio-channel-front-left.oga audio-test-signal.oga dialog-error.oga phone-incoming-call.oga service-logout.oga
audio-channel-front-right.oga audio-volume-change.oga dialog-information.oga phone-outgoing-busy.oga suspend-error.oga
audio-channel-rear-center.oga bell.oga dialog-warning.oga phone-outgoing-calling.oga trash-empty.oga
audio-channel-rear-left.oga camera-shutter.oga message-new-instant.oga power-plug.oga window-attention.oga
audio-channel-rear-right.oga complete.oga message.oga power-unplug.oga window-question.oga

Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga
Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/complete.oga
Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/device-added.oga
Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/device-removed.oga
Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/dialog-error.oga
Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/dialog-information.oga
Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/dialog-warning.oga
Tower1:~$ pw-play /usr/share/sounds/freedesktop/stereo/message-new-instant.oga
 
Code:
#!/bin/bash
# battery-watch.sh - Monitor battery and alert when low

# If battery is below 10%
THRESHOLD=10
# Check every 300 seconds (5 mins)
INTERVAL=300  
SOUND_BASE="/usr/share/sounds/freedesktop/stereo"
ALERT_SOUND="${SOUND_BASE}/alarm-clock-elapsed.oga"
BATTERY_PATH="/sys/class/power_supply/BAT0"

# Verify battery exists
if [[ ! -d "$BATTERY_PATH" ]]; then
    echo "No battery found at $BATTERY_PATH — are you on a laptop?"
    exit 1
fi

echo "Battery watch started (threshold: ${THRESHOLD}%, check every $((INTERVAL/60)) min)"

while true; do
    STATUS=$(cat "${BATTERY_PATH}/status")
    CAPACITY=$(cat "${BATTERY_PATH}/capacity")

    if [[ "$STATUS" != "Charging" && "$CAPACITY" -le "$THRESHOLD" ]]; then
        echo "[$(date '+%H:%M:%S')] WARNING: Battery at ${CAPACITY}% — PLUG IN!"
        pw-play "$ALERT_SOUND" 2>/dev/null
    else
        echo "[$(date '+%H:%M:%S')] Battery: ${CAPACITY}% (${STATUS})"
    fi

    sleep "$INTERVAL"
done
 
FYI I finally got cron working, for both audio and display alerts as the battery discharges

crontab -e
PATH=/usr/bin:/bin:/home/steven/.local/bin/
*/2 * * * * /home/steven/cronjobs/alert.sh >/dev/null 2>&1 (run every 2 minutes)
@reboot sleep 30 && /usr/local/bin/restart_cron.sh (Not sure this is necessary)

Script is located in /home/steven/cronjobs
Sound files were downloaded from some Star Trek website a few years ago

Attached is the .sh script, renamed .txt for upload

The persistent delay I see (Cron runs, but no alerts are generated by the script for about 5 minutes) may be due to Anacron. Not sure about this.

$ cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
HOME=/root
LOGNAME=root
# These replace cron's entries
#period in days delay in minutes job-identifier command
1 5 cron.daily run-parts --report /etc/cron.daily
7 10 cron.weekly run-parts --report /etc/cron.weekly
@monthly 15 cron.monthly run-parts --report /etc/cron.monthly
 

Attachments

Last edited:


Follow Linux.org

Members online


Top