<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof"><span style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" class="elementToProof">
<div class="cooked">
<p class="ContentPasted0 elementToProof">Hi!</p>
<p class="ContentPasted0 elementToProof"><br>
</p>
<p class="ContentPasted0 elementToProof">Some systems I have take some time to shut down, so I would like them to shut down if the UPS have been on battery for 2 minutes.
<br>
</p>
<p class="ContentPasted0 elementToProof"><br>
</p>
<p class="ContentPasted0">Internet has told me that <strong class="ContentPasted0">
SHUTDOWNCMD “/sbin/shutdown -h +0”</strong> in <strong class="ContentPasted0">/etc/nut/upsmon.conf</strong> will execute when the UPS is both OB (on battery) and LB (Low battery). Like in
<a href="https://wiki.ipfire.org/addons/nut/detailed" class="inline-onebox ContentPasted0" rel="noopener nofollow ugc">
wiki.ipfire.org - Detailed NUT Configuration</a> where it says</p>
<blockquote>
<p class="ContentPasted0">“SHUTDOWNCMD “/sbin/shutdown -h +0” this command will be triggered if UPS sends the BL (Battery Low) flag.”</p>
</blockquote>
<p class="ContentPasted0 elementToProof">But I would like to be on the safest side, and not hope “low battery” is enough time to shut down. So 2 minutes sound much more safe for paranoid me.</p>
<p class="ContentPasted0 elementToProof"><br>
</p>
<p class="ContentPasted0">I do not know much about NUT, but after some Googling, one idea I have is to in /etc/nut/upsmon.conf on client change to this</p>
<pre><code class="hljs language-sql ContentPasted0">NOTIFYCMD "/etc/nut/shutdown_script.sh"
NOTIFYFLAG ONBATT <span class="hljs-keyword ContentPasted0">EXEC</span>
</code></pre>
<p class="ContentPasted0">And then in /etc/nut/shutdown_script.sh have something like this</p>
<pre><code class="hljs language-bash ContentPasted0"><span class="hljs-meta ContentPasted0">#!/bin/bash</span>

poll_interval=5
wait_time=120
counter=0
no_wait_time=60
no_counter=0

<span class="hljs-keyword ContentPasted0">while</span> <span class="hljs-literal ContentPasted0">true</span>
<span class="hljs-keyword ContentPasted0">do</span>
  output=$(upsc qnapups@192.168.222.252 ups.status)
  <span class="hljs-keyword ContentPasted0">if</span> <span class="hljs-built_in ContentPasted0">echo</span> <span class="hljs-string ContentPasted0">"<span class="hljs-variable ContentPasted0">$output</span>"</span> | grep -q <span class="hljs-string ContentPasted0">"OB"</span>
  <span class="hljs-keyword ContentPasted0">then</span>
    counter=$((counter + poll_interval))
    no_counter=0
  <span class="hljs-keyword ContentPasted0">else</span>
    counter=0
    no_counter=$((no_counter + poll_interval))
  <span class="hljs-keyword ContentPasted0">fi</span>
  <span class="hljs-keyword ContentPasted0">if</span> [ <span class="hljs-variable ContentPasted0">$counter</span> -ge <span class="hljs-variable ContentPasted0">$wait_time</span> ]
  <span class="hljs-keyword ContentPasted0">then</span>
    <span class="hljs-built_in ContentPasted0">echo</span> <span class="hljs-string ContentPasted0">"Still outoput OB = On Battery, after 2 min. I will now shut down"</span>
        <span class="hljs-comment ContentPasted0"># Here I will shutdown the system </span>
        <span class="hljs-comment ContentPasted0"># "/sbin/shutdown -h +0"</span>
    <span class="hljs-built_in ContentPasted0">break</span>
  <span class="hljs-keyword ContentPasted0">fi</span>
  <span class="hljs-keyword ContentPasted0">if</span> [ <span class="hljs-variable ContentPasted0">$no_counter</span> -ge <span class="hljs-variable ContentPasted0">$no_wait_time</span> ]
  <span class="hljs-keyword ContentPasted0">then</span>
    <span class="hljs-built_in ContentPasted0">echo</span> <span class="hljs-string ContentPasted0">"Have not seen OB for 1 min, so quiting the script"</span>
    <span class="hljs-built_in ContentPasted0">break</span>
  <span class="hljs-keyword ContentPasted0">fi</span>
  <span class="hljs-built_in ContentPasted0">sleep</span> <span class="hljs-variable ContentPasted0">$poll_interval</span>
<span class="hljs-keyword ContentPasted0">done</span>
</code></pre>
<p class="ContentPasted0 elementToProof">But now I am starting to wonder if I am overdoing this. Surely there must already be a ready-made solution for this in NUT somewhere, but I have just searched with wrong words i Google.</p>
<p class="ContentPasted0 elementToProof"><br>
</p>
<p class="ContentPasted0 elementToProof">Have any of you done something similar with NUT, and what is the right way to do it?</p>
<p class="ContentPasted0 elementToProof"><br>
</p>
<p class="ContentPasted0 elementToProof">/raffe<br>
</p>
</div>
<br>
</span></div>
</body>
</html>