{"id":14,"date":"2011-12-13T04:48:27","date_gmt":"2011-12-13T04:48:27","guid":{"rendered":"http:\/\/news.innerfire.net\/?p=14"},"modified":"2018-07-25T15:26:59","modified_gmt":"2018-07-25T15:26:59","slug":"check-for-battery-power-with-linux","status":"publish","type":"post","link":"https:\/\/news.innerfire.net\/?p=14","title":{"rendered":"Check for battery power with Linux"},"content":{"rendered":"<p><a title=\"onbattery.tar.gz\" href=\"http:\/\/www.innerfire.net\/onbattery\/onbattery.tar.gz\" target=\"_blank\" rel=\"noopener\">onbattery.tar.gz <\/a><\/p>\n<p>I&#8217;ve been doing some work on my Debian Linux laptop lately and I have some nightly maintenance (disk check, btrfs tree balance) I want to do that will simply suck my battery dry if I&#8217;m not plugged into the wall.\u00a0 I did a quick web search to see how to check from a script to see if the system is on battery power or not and what I found was:<\/p>\n<ul>\n<li>Using outdated interfaces.<\/li>\n<li>fragile<\/li>\n<li>More complicated than it needed to be.<\/li>\n<\/ul>\n<p>So I ended up writing something that took far less time than the original search did.\u00a0 How did I do it you ask?<\/p>\n<p>Enter the acpi comm and.\u00a0 On old systems it uses proc and on new systems it uses sys.\u00a0 It cares so we don&#8217;t have to. It is not designed to be used in the way I want but writing a wrapper around it is easier than browsing either the proc or the sys interfaces on their own so I made this <a href=\"http:\/\/www.innerfire.net\/onbattery\/onbattery.tar.gz\" target=\"_blank\" rel=\"noopener\">simple wrapper around it.<\/a><\/p>\n<pre class=\"brush: bash; gutter: true\">#!\/bin\/sh\r\n# Script to check if we are on battery power.\r\n# By Gerhard Mack &lt;gmack@innerfire.net&gt;\r\n# Licensed in the public domain.\r\n\r\n#scan for any on-line adaptors\r\nacpi -a | grep \"on-line\" &gt; \/dev\/null 2&gt;&amp;1 \r\n\r\n#Grep will return 0 if it finds a matching line.\r\nif [ $? -eq 0 ]; then\r\n\t#we are on ac.  Return false and exit\r\n\texit 0 ;\r\nelse\r\n\t#we are on battery.  Return true and exit ;\r\n\texit 1 ;\r\nfi<\/pre>\n<p>Example usage in bash:<\/p>\n<p>onbattery<\/p>\n<pre class=\"brush: bash; gutter: true\">if [ $? -eq 1  ]; then\r\n        # we are on battery so abort before sucking the battery dry\r\n        exit\r\nfi<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>onbattery.tar.gz I&#8217;ve been doing some work on my Debian Linux laptop lately and I have some nightly maintenance (disk check, btrfs tree balance) I want to do that will simply suck my battery dry if I&#8217;m not plugged into the wall.\u00a0 I did a quick web search to see how to check from a script [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-scripts"],"_links":{"self":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14"}],"version-history":[{"count":14,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.innerfire.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}