7

My laptop unplugged stands 1 minute. Previous week my laptop cable, started not connecting good with the laptop.

I would like to play a certain sound everytime it gets's unplugged so I can hurry and connect them good.. Anyone?

4 Answers 4

4

As explained in this answer, you have to:

  1. cd into your home folder and create the directory .local/share/sounds:

    cd && mkdir -p .local/share/sounds
    
  2. cd into the newly created directory:

    cd .local/share/sounds
    
  3. place the desired sound inside the directory, renaming it as power-unplug.wav (for example in this way):

    ln -s /usr/share/sounds/alsa/Noise.wav power-unplug.wav
    
  4. test the event with:

    canberra-gtk-play -i power-unplug
    

I suppose that, at this point, you have to logout and log back in to have the event work properly. Try it and see if it works. You can find more events and sound names here.

1
  • 1
    All of that works, and the sound DOES play in step 4, but does not play when the cord is actually unplugged. I'm on Ubuntu 16.04 Commented Jan 20, 2021 at 5:48
2

On my Ubuntu 16.04 system, the sounds didn't fire in response to the actual events, although they would play from the test.

Eventually I wrote a script: ~/checkpower.sh

#! /bin/bash
echo "monitoring"
power=$(cat /sys/class/power_supply/BAT0/status)
while true; do
  actual=$(cat /sys/class/power_supply/BAT0/status)
  # echo $actual
  if [ $actual != $power ]; then
     power=$actual
     echo $actual
  fi
  if [ $actual == "Discharging" ]; then
     echo "discharging"
     /usr/bin/speaker-test --frequency 2000 --test sine -p 1 -P 2 -l 1
  fi

  sleep 10
done

chmod +x ~/checkpower.sh

And setup a service: /etc/systemd/system/checkpower.service

[Unit]
Description=Check to see if battery is discharging and warn user

[Service]
Type=simple
ExecStart=/home/<myusername>/checkpower.sh
#in this line specify the path to the script.
User=<myusername>
#Environment=DISPLAY=:0
#Group=audio
Environment="XDG_RUNTIME_DIR=/run/user/1000"

[Install]
WantedBy=multi-user.target

Obviously, change <myusername> to your actual username or change that path to wherever you put the script. Should probably be in /usr/bin. Then: sudo systemctl start checkpower

and then unplug the laptop... wait until it screams. I could systemctl status checkpower to see the echos and errors. 'q' out of the status and go back in to update. There is probably a better way to do that.

So once that worked, (and I had to change the BAT1 to BAT0 on mine) then I did a sudo systemctl enable checkpower and it seems to be working ok.

Updated for Ubuntu 22.04: I had to add the User= line and the Environment setting which I copied from the value returned by echo $XDG_RUNTIME_DIR

Things I wanted to do and couldn't:

  • aplay, canberra-gtk-play all that stuff won't work when running as a service. Services run as root and root can't find the hardware. I guess speaker-test works because it uses the default? No idea.
  • Same for using e.g. /usr/bin/notify-send -u critical "Monitoring Battery" to display a notification. It can't find the display, and there is some weird problem with DBUS_SESSION_BUS_ADDRESS... see: Notify-send doesn't work from crontab
0

You can use 'cuttlefish' and 'vlc' (for example) from the Ubuntu-Software-Centre. With this fine little application you can use diffrent situations as a trigger for specific actions to be carried out.

Like for you it would be:

start cuttlefish

  • create new 'rule'
    1. choose 'activate by stimulus'
    2. choose stimulus -> hardware -> 'power cable unplugged'
    3. then on 'reactions' tab choose 'start application (advanced mode)'
    4. choose cvlc (need to have vlc installed) as executable
    5. type the path to the desired sound (should be short) into the parameter field

Now your sound should be played every time you unplug your power cable.

1
  • Update: Cuttlefish is not available for versions 14.04 and higher.
    – Bib-lost
    Commented Sep 9, 2016 at 9:19
-1
#!/usr/bin/env python

import commands
import pynotify
from threading import Timer


def battery_check():

    rem = float(commands.getoutput("grep \"^remaining capacity\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'"))
    full = float(commands.getoutput("grep \"^last full capacity\" /proc/acpi/battery/BAT0/info | awk '{ print $4 }'"))
    state = commands.getoutput("grep \"^charging state\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'")

    percentage = int((rem/full) * 100)

    if state == "discharging":
        pynotify.init("Battery Alert!")
        notification = pynotify.Notification("Battery "+state,str(percentage)+"%","/usr/share/icons/gnome/32x32/status/battery-low.png")
        notification.show()

    timer = Timer(300.0,battery_check)
    timer.start()

if __name__ == "__main__": battery_check()

Download here.

You must log in to answer this question.

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