<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>scripts &amp;mdash; sandeepk</title>
    <link>https://blogs.dgplug.org/sandeepk/tag:scripts</link>
    <description></description>
    <pubDate>Thu, 30 Apr 2026 10:42:45 +0000</pubDate>
    <item>
      <title>Automatic Notification for the Laptop Battery Level</title>
      <link>https://blogs.dgplug.org/sandeepk/automatic-notification-for-the-laptop-battery-level</link>
      <description>&lt;![CDATA[While working from home one of the issue I faced that my laptop battery charger adapter is always remain plugged in almost all the time, due to which I have to replace my laptop battery. To deal with the problem I have now written a script to notify me about the laptop battery charging level if it goes above 85 % and below 20%.&#xA;&#xA;! /bin/bash                                                                                                                                          &#xA;&#xA;while true&#xA;do&#xA;&#xA;    batterylevel=acpi -b | grep -o &#39;[0-9]&#39; | sed -n  2p&#xA;    acpower=cat /sys/class/powersupply/AC/online&#xA;&#xA;    #If above command raise an error &#34; No such file or directory&#34; try the below command.&#xA;    #acpower=cat /sys/class/powersupply/ACAD/online&#xA;    if [ $acpower -gt 0 ]; then&#xA;        if [ $batterylevel -ge 85 ]; then&#xA;            notify-send &#34;Battery Full&#34; &#34;Level: ${batterylevel}%&#34;&#xA;        fi&#xA;    else&#xA;        if [ $batterylevel -le 20 ]; then&#xA;            notify-send --urgency=CRITICAL &#34;Battery Low&#34; &#34;Level: ${batterylevel}%&#34;&#xA;        fi&#xA;    fi&#xA;    sleep 120&#xA;&#xA;done&#xA;The important commands which I want to break down to explain actually what they are doing.&#xA;&#xA;First is acpi which tell us about the battery information and other ACPI information&#xA;grep command is used to extract the integer value from the acpi command&#xA;sed is used to get the second value from the grep result.&#xA;&#xA;    acpi -b&#xA;Battery 0: Discharging, 54%, 02:03:37 remaining&#xA;    acpi -b | grep -o &#39;[0-9]&#39;&#xA;0&#xA;54&#xA;02&#xA;04&#xA;41&#xA;    acpi -b | grep -o &#39;[0-9]&#39; | sed -n  2p&#xA;54&#xA;After that, we check that the charger is plugged in or not, based on that we check that the battery level does not exceed the described limit, if so is the case send the notification.&#xA;Then we check for the battery low indication which sends the notification if the battery level less than 20 %.&#xA;These condition is put in a continuous loop to check after a sleep time of 120s.&#xA;&#xA;To make this script run automatically you have to assign the execution permission and specifies the execution command in the ~/.profile and reboot the system.&#xA;    sudo chmod +x /path/to/battery-notification.sh&#xA;you can find my notes on shell commands here&#xA;&#xA;thanks shrini for pointing out issue for Ubuntu 20 in lineCheers!&#xA;&#xA;#100DaysToOffload #automation #scripts&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>While working from home one of the issue I faced that my laptop battery charger adapter is always remain plugged in almost all the time, due to which I have to replace my laptop battery. To deal with the problem I have now written a script to notify me about the laptop battery charging level if it goes above 85 % and below 20%.</p>

<pre><code class="language-bash">#! /bin/bash                                                                                                                                          

while true
do

    battery_level=`acpi -b | grep -o &#39;[0-9]*&#39; | sed -n  2p`
    ac_power=`cat /sys/class/power_supply/AC/online`

    #If above command raise an error &#34; No such file or directory&#34; try the below command.
    #ac_power=`cat /sys/class/power_supply/ACAD/online`
    if [ $ac_power -gt 0 ]; then
        if [ $battery_level -ge 85 ]; then
            notify-send &#34;Battery Full&#34; &#34;Level: ${battery_level}%&#34;
        fi
    else
        if [ $battery_level -le 20 ]; then
            notify-send --urgency=CRITICAL &#34;Battery Low&#34; &#34;Level: ${battery_level}%&#34;
        fi
    fi
    sleep 120

done
</code></pre>

<p>The important commands which I want to break down to explain actually what they are doing.</p>
<ul><li>First is <em>acpi</em> which tell us about the battery information and other ACPI information</li>
<li><em>grep</em> command is used to extract the integer value from the <em>acpi</em> command</li>
<li><em>sed</em> is used to get the second value from the <em>grep</em> result.</li></ul>

<pre><code class="language-bash">&gt;&gt; acpi -b
Battery 0: Discharging, 54%, 02:03:37 remaining
&gt;&gt;acpi -b | grep -o &#39;[0-9]*&#39;
0
54
02
04
41
&gt;&gt;acpi -b | grep -o &#39;[0-9]*&#39; | sed -n  2p
54
</code></pre>
<ul><li>After that, we check that the charger is plugged in or not, based on that we check that the battery level does not exceed the described limit, if so is the case send the notification.</li>
<li>Then we check for the battery low indication which sends the notification if the battery level less than 20 %.</li>
<li>These condition is put in a continuous loop to check after a sleep time of <em>120s</em>.</li></ul>

<p>To make this script run automatically you have to assign the execution permission and specifies the execution command in the <strong>~/.profile</strong> and reboot the system.</p>

<pre><code class="language-bash">&gt;&gt; sudo chmod +x /path/to/battery-notification.sh
</code></pre>

<p>you can find my notes on shell commands <a href="https://blogs.dgplug.org/sandeepk/tag:shellrun" rel="nofollow">here</a></p>

<p><em>thanks <a href="https://mastodon.social/@tshrinivasan" rel="nofollow">shrini</a> for pointing out issue for Ubuntu 20 in line<code>ac_power=`cat /sys/class/power_supply/AC/online`</code>  :)</em>
Cheers!</p>

<p><a href="/sandeepk/tag:100DaysToOffload" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">100DaysToOffload</span></a> <a href="/sandeepk/tag:automation" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">automation</span></a> <a href="/sandeepk/tag:scripts" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">scripts</span></a></p>
]]></content:encoded>
      <guid>https://blogs.dgplug.org/sandeepk/automatic-notification-for-the-laptop-battery-level</guid>
      <pubDate>Sun, 21 Feb 2021 17:20:19 +0000</pubDate>
    </item>
  </channel>
</rss>