|
|
@ -1,34 +1,44 @@ |
|
|
|
<h1>Shell scripts</h1> |
|
|
|
<h1>Shell Scripts</h1> |
|
|
|
<p>You can easily add SITE_NAME monitoring to a shell script. All you |
|
|
|
have to do is make a HTTP request at the end of the script. curl and wget |
|
|
|
are two common command line HTTP clients for that.</p> |
|
|
|
<h2>Using curl</h2> |
|
|
|
have to do is make a HTTP request at the end of the script. |
|
|
|
<a href="https://curl.haxx.se/docs/manpage.html">curl</a> and |
|
|
|
<a href="https://www.gnu.org/software/wget/manual/wget.html">wget</a> |
|
|
|
are two common command line HTTP clients you can use.</p> |
|
|
|
<div class="highlight"><pre><span></span><span class="c1"># Sending a HTTP GET request with curl:</span> |
|
|
|
curl --retry <span class="m">3</span> PING_URL |
|
|
|
|
|
|
|
<span class="c1"># Silent version (no stdout/stderr output unless curl hits an error):</span> |
|
|
|
curl -fsS --retry <span class="m">3</span> PING_URL |
|
|
|
|
|
|
|
<span class="c1"># Sending a HTTP GET request with wget:</span> |
|
|
|
wget PING_URL -O /dev/null |
|
|
|
</pre></div> |
|
|
|
|
|
|
|
|
|
|
|
<h2>Signalling Failure from Shell Scripts</h2> |
|
|
|
<p>You can append <code>/fail</code> to any ping URL and use the resulting URL to actively |
|
|
|
signal a failure. The below example:</p> |
|
|
|
<ul> |
|
|
|
<li>runs <code>/usr/bin/certbot renew</code></li> |
|
|
|
<li>if the certbot command is successful (exit code 0), send HTTP GET to <code>PING_URL</code></li> |
|
|
|
<li>otherwise, send HTTP GET to <code>PING_URL/fail</code></li> |
|
|
|
</ul> |
|
|
|
<div class="highlight"><pre><span></span><span class="ch">#!/bin/sh</span> |
|
|
|
|
|
|
|
<span class="c1"># Exit immediately if any command exits with a non-zero status:</span> |
|
|
|
<span class="nb">set</span> -e |
|
|
|
<span class="c1"># Payload here:</span> |
|
|
|
/usr/bin/certbot renew |
|
|
|
<span class="c1"># Ping SITE_NAME</span> |
|
|
|
curl --retry <span class="m">3</span> <span class="s2">"PING_URL</span><span class="k">$(</span><span class="o">[</span> <span class="nv">$?</span> -ne <span class="m">0</span> <span class="o">]</span> <span class="o">&&</span> <span class="nb">echo</span> -n /fail<span class="k">)</span><span class="s2">"</span> |
|
|
|
</pre></div> |
|
|
|
|
|
|
|
<span class="c1"># Do the work here</span> |
|
|
|
<span class="nb">echo</span> <span class="s2">"Pretending to to make backups..."</span> |
|
|
|
sleep <span class="m">5</span> |
|
|
|
<span class="nb">echo</span> <span class="s2">"Backup complete!"</span> |
|
|
|
|
|
|
|
<span class="c1"># As the last thing, ping SITE_NAME using curl:</span> |
|
|
|
<span class="hll">curl --retry <span class="m">3</span> PING_URL |
|
|
|
</span></pre></div> |
|
|
|
|
|
|
|
|
|
|
|
<h2>Using wget</h2> |
|
|
|
<h2>Logging Command Output</h2> |
|
|
|
<p>When pinging with HTTP POST, you can put extra diagnostic information in request |
|
|
|
body. If the request body looks like a valid UTF-8 string, SITE_NAME |
|
|
|
will accept and store first 10KB of the request body.</p> |
|
|
|
<p>In the below example, certbot's output is captured and submitted via HTTP POST:</p> |
|
|
|
<div class="highlight"><pre><span></span><span class="ch">#!/bin/sh</span> |
|
|
|
|
|
|
|
<span class="c1"># Exit immediately if any command exits with a non-zero status:</span> |
|
|
|
<span class="nb">set</span> -e |
|
|
|
|
|
|
|
<span class="c1"># Do the work here</span> |
|
|
|
<span class="nb">echo</span> <span class="s2">"Pretending to to generate reports..."</span> |
|
|
|
sleep <span class="m">5</span> |
|
|
|
<span class="nb">echo</span> <span class="s2">"Report generation complete!"</span> |
|
|
|
|
|
|
|
<span class="c1"># As the last thing, ping SITE_NAME using wget:</span> |
|
|
|
<span class="hll">wget PING_URL -O /dev/null |
|
|
|
</span></pre></div> |
|
|
|
<span class="nv">m</span><span class="o">=</span><span class="k">$(</span>/usr/bin/certbot renew <span class="m">2</span>><span class="p">&</span><span class="m">1</span><span class="k">)</span> |
|
|
|
curl -fsS --retry <span class="m">3</span> -X POST --data-raw <span class="s2">"</span><span class="nv">$m</span><span class="s2">"</span> PING_URL |
|
|
|
</pre></div> |