<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Colas.Nahaboo.net - hardware</title>
    <subtitle>Colas Nahaboo personal site, with discussions about programming code, web and computing topics, surfing and SUPing, and various musings.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://colas.nahaboo.net/tags/hardware/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://colas.nahaboo.net"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-08-12T00:00:00+00:00</updated>
    <id>https://colas.nahaboo.net/tags/hardware/atom.xml</id>
    <entry xml:lang="en">
        <title>ups-monitor.sh: staged shutdowns when the power goes out</title>
        <published>2026-08-12T00:00:00+00:00</published>
        <updated>2026-08-12T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://colas.nahaboo.net/code/ups-monitor-staged-shutdowns-when-the-power-goes-out/"/>
        <id>https://colas.nahaboo.net/code/ups-monitor-staged-shutdowns-when-the-power-goes-out/</id>
        
        <content type="html" xml:base="https://colas.nahaboo.net/code/ups-monitor-staged-shutdowns-when-the-power-goes-out/">&lt;img src=&quot;ups-monitor-overview.png&quot;&gt;
&lt;p&gt;I released &lt;strong&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/ColasNahaboo/ups-monitor.sh&quot;&gt;ups-monitor.sh&lt;/a&gt;&lt;/strong&gt;, a bash script that decides what happens when the power goes out.&lt;/p&gt;
&lt;p&gt;My UPS powers my servers, NAS, main switch and wifi access point. I want to cut off first my workstations in a clean way, to keep as much battery power to provide my main file server access via wifi for my phones and tablets.&lt;/p&gt;
&lt;p&gt;Configuring &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://networkupstools.org/&quot;&gt;NUT&lt;/a&gt; scripts for this seemed verbose and complex for my small setup, so I basically coded a simple but flexible bash control layer over the NUT infrastructure&lt;/p&gt;
&lt;h2 id=&quot;the-staged-shutdown&quot;&gt;The staged shutdown&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-staged-shutdown&quot; aria-label=&quot;Anchor link for: the-staged-shutdown&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;So the script shuts things down in stages, as the battery drains:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;90% battery&lt;/strong&gt;: shut down the non-essential &quot;first&quot; servers — storage, backups, the toy boxes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;80% battery&lt;/strong&gt;: shut down the &quot;extra&quot; servers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;20% battery&lt;/strong&gt;: shut down the monitoring host itself, and put the UPS in standby.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The UPS on the final step powers everything off and goes to sleep; when the mains come back, it wakes up, powers the equipment, and the machines can boot again. Recovery is automatic.&lt;/p&gt;
&lt;p&gt;The staging is fully configurable: the thresholds and the server lists live in &lt;code&gt;/etc/ups-monitor.conf&lt;/code&gt;, and servers can be prefixed &lt;code&gt;h:&lt;/code&gt; to hibernate instead of shutdown, or &lt;code&gt;wh:&lt;/code&gt;/&lt;code&gt;ws:&lt;/code&gt; for Windows hosts.&lt;/p&gt;
&lt;h2 id=&quot;the-config-is-bash&quot;&gt;The config is bash&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-config-is-bash&quot; aria-label=&quot;Anchor link for: the-config-is-bash&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As I often set up my scripts, the config file is &lt;em&gt;sourced&lt;/em&gt; by the script, so it is not a list of options in some syntax, it is pure bash. You can override any variable — and any &lt;strong&gt;function&lt;/strong&gt;. Redefine &lt;code&gt;info&lt;/code&gt; to text you instead of mailing you. Redefine &lt;code&gt;remoteshut&lt;/code&gt; to do whatever exotic thing your network needs. You can even write &quot;pseudo servers&quot; as &lt;code&gt;funcname:params&lt;/code&gt; in the server lists, and the script will run that function before shutting down — say, dump the databases before the machine dies. The anti-framework approach, same as my &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/ColasNahaboo/colas-bash-lib&quot;&gt;colas-bash-lib&lt;/a&gt; collection.&lt;/p&gt;
&lt;p&gt;It just polls the UPS every five seconds, so bash provides simplicity, stability and flexibility with a negligible overhead.&lt;/p&gt;
&lt;h2 id=&quot;goodies&quot;&gt;Goodies&lt;a class=&quot;zola-anchor&quot; href=&quot;#goodies&quot; aria-label=&quot;Anchor link for: goodies&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A dry-run mode toggled by &lt;code&gt;touch /tmp/NOUPSMON&lt;/code&gt; — no restart needed — so you can unplug the UPS and watch the logs before trusting it for real.&lt;/li&gt;
&lt;li&gt;Email alerts for power lost and restored, overload, and battery-to-replace. AVR events (TRIM/BOOST) logged with the exact input voltage, for info. Logs rotated monthly, and a &lt;code&gt;clean&lt;/code&gt; command for the cron.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;compatible-ups&quot;&gt;Compatible UPS&lt;a class=&quot;zola-anchor&quot; href=&quot;#compatible-ups&quot; aria-label=&quot;Anchor link for: compatible-ups&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One advice: choose a &lt;strong&gt;pure sine wave&lt;/strong&gt; UPS, otherwise some active-PFC power supplies may panic and shut down abruptly when the UPS switches to batteries. Save yourself the uncertainty.&lt;/p&gt;
&lt;h2 id=&quot;installation-usage-and-more-info&quot;&gt;Installation, Usage, and more info&lt;a class=&quot;zola-anchor&quot; href=&quot;#installation-usage-and-more-info&quot; aria-label=&quot;Anchor link for: installation-usage-and-more-info&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https://github.com/ColasNahaboo/ups-monitor.sh&quot;&gt;github.com/ColasNahaboo/ups-monitor.sh&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note: This is AI-co-advised: chatting with Gemini helped me as a UPS novice, but it did not write the code or the docs.&lt;/p&gt;
</content>
        
    </entry>
</feed>
