Browse - Programming Tips - How can my bash script detect if the computer is on battery power (or not)?

Date: 2013mar4
Language: bash
OS: Linux

Q.  How can my bash script detect if the computer is on battery power (or not)?

A.  You can use the on_ac_power command like this:

	if on_ac_power; then
		echo 'On AC -- ie plugged in'
	else
		echo 'Using battery'
		# Maybe do less work
	fi

On RedHat/Fedora/CentOS this is in the pm-utils package