I want my computer to play a custom sound file when my battery is low (say 10%). How can I do this? Thanks
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
To install it
To get battery status from command line you can use this command
Now you can put all these things in a shell script (and also an
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!
I just wanted to reply as I used the advice on this page for configuring my own
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:
File to open to write the command:
Command to enter at end of the file:
Explanation of the above
Asterisks at the start
The five asterisks represent the frequency at which to run the commands.
means 'run this every minute'.
Most of you, I imagine, are very familiar with IF/THEN statements. The FI at the end concludes the IF statement
Playing the audio:
this simply uses
Output battery info:
this simply outputs the battery status. On my laptop it yields results such as:
Pipe to awk command
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,
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!
Okay... so it turns out that when my laptop is charging, the output of
whereas on battery is
so using the parameter mentioned initially of
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
I read up a little more on the output of the
SO... to make it read the fourth value, the command changes from
or in its full glory:
Again I hope that helps someone! Good luck and thanks for reading!
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:
as you can see, all I've done is inserted the command string
Again, good luck all!
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 (
So basically replace
Therefore, the command you really REALLY need is:
phew hope that sorts it for you as it has for me!! (eventually!)
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.
Next,while still in the terminal, type
Hit Control-O (the letter) and Enter to save. Hit Control-x to exit
Extra stuff you might want to read...
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
Save the following command in
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.
Step 4: Execute this file using cron by adding the following command to the end of the file opend by
(Don't forget to have an empty line after this command)