13

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
  • 133
  • 1
  • 6

1 Answers1

18

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
  • 174,089
  • 51
  • 332
  • 407