12

I need to check in a bash script running when my laptop starts if the AC adapter is plugged or not. Is this possible?

user222682
  • 123
  • 1
  • 6

1 Answers1

17

You can use acpi with -a argument. To see how it works, run in your terminal:

acpi -a

By default, acpi package is not installed in Ubuntu, but is very easy and quickly to install from your terminal using the following command:

sudo apt-get install acpi

Then, in your script you can use for example:

ac_adapter=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)

if [ "$ac_adapter" = "on" ]; then
    notify-send "AC Adapter" "The AC Adapter is on."
else
    notify-send "AC Adapter" "The AC Adapter is off."
fi

To make the script to run at start up, just add a new entry in your crontab list (using crontab -e command) as follow:

@reboot DISPLAY=:0.0 /path/to/your/script
Radu Rădeanu
  • 166,822
  • 48
  • 327
  • 400
  • Nice answer, my next question would have been about battery state, but `acpi` solve this problem too. Thank you! – user222682 Dec 06 '13 at 08:58