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!
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...
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:
File to open to write the command:
Command to enter at end of the file:
Explanation of the above
Asterisks at the start There 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 mpg123 to play a file called Alarm.mp3 located in my home directory /home/andy/
Output battery info:
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
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
$(NF-2) outputs '36%' whereas with the laptop on charge,
$(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
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
(the &&'s just bind multiple commands together)
Again, good luck all!
Okay, SO.... one final (hopefully!) edit ... apparently, the percentage sign is a special sign when running crontab tasks. Therefore, you have to 'escape' the percentage sign by putting a backslash '\' in front of each '%' so it isn't (mis)interprested by the shell.
So basically replace
Therefore, the command you really REALLY need .... is
phew hope that sorts it for you as it has for me!! (eventually!)