How to check if power cable is connected from the command line?

9

1

I'm running ubuntu on my notebook and I need a way to check (from the command line) if the power cable is connected. Is there any system file that keeps log of this? or is there a bash command that will help me?

I need this for a script that will behave differently depending on whether the power cord is plugged in or not.

Malabarba

Posted 2010-06-09T22:48:46.313

Reputation: 7 588

Answers

7

$ cat /proc/acpi/ac_adapter/ACAD/state
state:                   on-line

or somthing similar depending on your architecture. There are many exports of machine state hiding in the /proc psuedo-directory.

msw

Posted 2010-06-09T22:48:46.313

Reputation: 3 287

Works exactly like that. Only it was /proc/acpi/ac_adapter/AC/state in my case. – Malabarba – 2010-06-10T01:51:54.787

12

$ cat /sys/class/power_supply/ADP1/online 
1
 [pull power]
$ cat /sys/class/power_supply/ADP1/online 
0

The value to put in place of ADP1 may vary.

Ignacio Vazquez-Abrams

Posted 2010-06-09T22:48:46.313

Reputation: 100 516

Works exactly like that as well, only with "/sys/class/power_supply/AC/online" for me. I marked the other answer as accepted 'cause it was first, but both worked equally well. – Malabarba – 2010-06-10T01:53:45.503

I only have a BAT0 directory in /sys/class/power_supply. Any idea why? – user1575682 – 2019-05-02T00:21:04.367

3

I use something similar to:

acpi -a | grep AC | cut -d: -f2

in order to display the battery status on the wmii status bar.

mrucci

Posted 2010-06-09T22:48:46.313

Reputation: 8 398

This one didn't work with me because the acpi command wasn't installed. I didn't install it because the other methods were more practical, but it probably works fine as well. – Malabarba – 2010-06-10T01:55:16.093