Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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!

share|improve this answer
3  
Thats right, only the cool people use mpg123 :) –  Scott Goodgame May 23 at 3:01

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.