15

I want my computer to play a custom sound file when my battery is low (say 10%). How can I do this? Thanks

4 Answers 4

7

I just wanted to reply as I used the advice on this page for configuring my own crontab -e job.

I'm running Lubuntu (trusty) and the above didn't work for me, but with a little tweaking it did. My level of (in)experience is that I'm relatively capable of reading basic scripts and commands but less able to write them from scratch in linux disto's. I mention that because if I can cobble together a working battery alarm, most of you reading this should be able to as well!

SO.... the script and a basic explanation....

As per the posts above by Glutanimate and Scott Goodgame (thanks guys!), I basically did the following:

To get mpg123 and be able to play audio:

sudo apt-get install acpi mpg123

File to open to write the command:

crontab -e

Command to enter at end of the file:

*/1 * * * * if [ `acpi -b | awk ' { print ( $(NF-2)-0)}'`  -lt "15" ] ; then mpg123 /home/andy/Alarm.mp3 ; fi

Explanation of the above

Asterisks at the start

The five asterisks represent the frequency at which to run the commands.

  • First star = Minutes: 0-59
  • Second star = Hours: 0-23
  • Third star = Day of Month: 0 - 31
  • Fourth star = Month: 0 - 12
  • Fifth star = Day of Week: 0 - 6 (0 means Sunday)

*/1 * * * *

means 'run this every minute'.

IF/THEN/FI:

IF [ stuff inside these brackets ] ; THEN do something ; FI

Most of you, I imagine, are very familiar with IF/THEN statements. The FI at the end concludes the IF statement

Playing the audio:

mpg123 /home/andy/Alarm.mp3

this simply uses mpg123 to play a file called Alarm.mp3 located in my home directory /home/andy/.

Output battery info:

acpi -b

this simply outputs the battery status. On my laptop it yields results such as:

Battery 0: Discharging, 74%, 02:35:18 remaining

Pipe to awk command

 | awk ' { print ( $(NF-2)-0)}'`  -lt "15"

Okay, so we're piping the output of the battery status above and printing the second-from-the-end value (NF-2) which in the above example is '74%'. However, we need to remove the 'percentage' -0. We want to compare the current value with the value 15 -lt "15". So to sum it up in plain English,

IF the value of the battery is LESS THAN 15% THEN play an audio file

I appreciate I've dumbed it down somewhat and perhaps my explanation could be improved on, but hopefully it'll help someone else understand how to add an alarm for the battery and avoid their laptops from just powering off all of a sudden!

Good luck!


EDIT

Okay... so it turns out that when my laptop is charging, the output of

acpi -b

is:

Battery 0: Charging, 35%, 01:04:08 until charged

whereas on battery is

Battery 0: Discharging, 36%, 01:22:36 remaining

so using the parameter mentioned initially of

print ( $(NF-2)-0)

outputs different results, because remember, the NF-value bit specifies how many items backwards from the end of the output to display, so with the output on battery of

Battery 0: Discharging, 36%, 01:22:36 remaining

$(NF-2) outputs '36%' whereas with the laptop on charge,

Battery 0: Charging, 35%, 01:04:08 until charged

$(NF-2) outputs '00:50:12', i.e. the amount of time remaining until charged.

The -0 after $(NF-2) minuses zero from the value, so when the value is a percent -0 changes it from something like '36%' to '36'. When the output value is perceived as non numerical, such as the time output of '01:04:08' as above, the value -0 returns is '0'. So you can see my issue - when my laptop is on charge, the output value is always '0'. Since I want to play an alarm when the output value is less than 15, it translates to a battery alarm that goes off every one minute when on charge!

I read up a little more on the output of the awk command and it turns out I didn't need to make it read the second value from the end, I could start from the beginning of the output and skip forward. This is much better as my battery status begins the same whether it is on charge or on battery in terms of number of parameters, i.e. the fourth parameter from the beginning is always the value of charge in percent.

SO... to make it read the fourth value, the command changes from

awk ' { print ($(NF-2)-0)}'

to

awk ' { print ($4)-0}'

or in its full glory:

*/1 * * * * if [ `acpi -b | awk ' { print ($4)-0}'`  -lt "15" ] ; then mpg123 /home/andy/Alarm.mp3 ; fi

Again I hope that helps someone! Good luck and thanks for reading!


BONUS EDIT

Okay, so a little additional poking around and I also worked out how to set the volume to 100% and unmute before playing the alarm - because let's face it, what's the point of a battery alarm if you can't hear it because you've set the volume to low or muted it?!

Anyway, the command you need is:

*/1 * * * * if [ `acpi -b | awk ' { print ($4)-0}'`  -lt "15" ] ; then pactl set-sink-volume 0 100% && pactl set-sink-mute 0 0 && mpg123 /home/andy/Alarm.mp3 ; fi

as you can see, all I've done is inserted the command string

pactl set-sink-volume 0 100% && pactl set-sink-mute 0 0 &&

(the &&'s just bind multiple commands together)

Again, good luck all!

LAST EDIT

Okay, So... one final (hopefully!) edit... apparently, the percentage sign is a special sign when running cron tasks. Therefore, you have to 'escape' the percentage sign by putting a backslash (\) in front of each % so it isn't (mis)interpreted by the shell.

So basically replace

100%

with

100\%

Therefore, the command you really REALLY need is:

*/1 * * * * if [ `acpi -b | awk ' { print ($4)-0}'`  -lt "15" ] ; then pactl set-sink-volume 0 100\% && pactl set-sink-mute 0 0 && mpg123 /home/andy/Alarm.mp3 ; fi

phew hope that sorts it for you as it has for me!! (eventually!)

1
  • This is a great explanation! Unfortunately, it also plays a sound every minute when charging too.
    – OpnSrcFan
    Dec 24, 2019 at 18:10
4

You can set up a cron job which will check for battery status after every 5/10 min and then depending on the battery percentage it will play some sound.

To play the sound you can use

mpg123 filename.mp3

To install it

sudo apt-get install mpg123

To get battery status from command line you can use this command

upower -i /org/freedesktop/UPower/devices/battery_BAT0

using grep you can extract the exact info as follows

upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage"

Now you can put all these things in a shell script (and also an if condition to check if battery status is lower than 10%) and just give path of this shell script to cron job.

so after every 5 mins (You can change this interval) your battery status will be checked and if battery is lower than 10% your provided sound file will be played!

Hope this helps!

1
  • 3
    Thats right, only the cool people use mpg123 :) May 23, 2014 at 3:01
4

One easy way to do this is to add a simple check to your crontab (cron is a system to run things on a regular, user-defined basis in the background.)

First you need to install a command line mp3 player and acpi to be able to read battery state.

From a terminal run the following to install the packages.

sudo apt-get install acpi mpg123

Next,while still in the terminal, type crontab -e and enter the following to the end of the file. Just change the user to your user name and the .mp3 to your filename in your home directory.

*5 * * * * if [ `acpi |awk '{print substr($NF, 0, length($NF)-1)}'`  -lt "10" ] ; then mpg123 /home/user/SuperCoolSound.mp3   ;fi

Hit Control-O (the letter) and Enter to save. Hit Control-x to exit

Extra stuff you might want to read...

Ubuntu CronHowto

Ubuntu Guide

1
  • Did not work here: ➜ ~ if [ acpi |awk '{print substr($NF, 0, length($NF)-1)}' -lt "10" ] ; then aplay /home/felipelalli/Music/siren.wav ;fi [: integer expression expected: remainin ➜ ~ acpi Battery 0: Discharging, 9%, 00:17:56 remaining
    – Felipe
    Mar 12, 2015 at 2:34
2

Actually I used the answer of Andy Pyne but it did not work for me. The cron executed the code, but it did not play the sound. Finally I have found the solution and sharing it here:

Step 1: Install mpg3

sudo apt-get install acpi mpg123

Step 2: Save the following command in ~/bin/battery_alert

#!/bin/bash
PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [ `acpi -b | awk ' { print ($3)}'`  == "Discharging," ] ; then
    # Discharging
    # Monitor for low battery
    if [ `acpi -b | awk ' { print ($4)-0}'`  -le "15" ] ; then
        pactl set-sink-volume 0 75\% && pactl set-sink-mute 0 0 && mpg123 /home/user/battery_low.mp3 ;
    fi
else
    # Charging
    if [ `acpi -b | awk ' { print ($4)-0}'`  -eq "100" ] ; then
        # Fully charged
        pactl set-sink-volume 0 75\% && pactl set-sink-mute 0 0 && mpg123 /home/user/battery_full.mp3 ;
    fi
fi

This script plays battery_low.mp3 if the laptop is discharging and charge is less than or equal 15%. (If AC power is connected, it will not alert you). This code also alerts you, if the charge is 100%. If you do not want an alert for fully charged state, remove the else part from this code.

Step 3: Make the file executable using the following command.

chmod +x ~/bin/battery_alert

Step 4: Execute this file using cron by adding the following command to the end of the file opend by crontrab -e command.

*/5 * * * * /home/user/bin/battery_alert

(Don't forget to have an empty line after this command)

1
  • Hi, I followed this instruction and completed without any error, just changed the spelling crontrab, but not working. Can anybody help me? My doubt in the path which is given inside crontab: */5 * * * * /home/user/bin/battery_aler. Ubuntu 18.04.5
    – Jayapramod
    Sep 24, 2020 at 12:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .