<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>scripting &amp;mdash; alan</title>
    <link>https://www.franzoni.eu/alan/tag:scripting</link>
    <description>Alan&#39;s musings</description>
    <language>en</language>
    <pubDate>Tue, 17 Nov 2026 00:52:48 +0000</pubDate>
    <item>
      <title>Quick log for bash scripts, with line limit</title>
      <link>https://www.franzoni.eu/alan/quick-log-for-bash-scripts-with-line-limit</link>
      <description>&lt;![CDATA[Sometimes you need to write a quick shell script; many a time, you need to put it on a server, and launch it every now and then, or run it in a cron job. That script may end up doing something important and/or altering the state of the server machine.&#xA;&#xA;If that script starts being using often enough, you&#39;ll want logging as well, and that can be a complex beast to tame, for logs require rotation and deletion, otherwise the beast will eat up&#xA;all your disk space. Setting up logrotate for a single shell script looks like an overkill to me most of the times.&#xA;&#xA;So what? Many a times logging will be neglected, and you&#39;ll lose the ability to track what happened and why.&#xA;&#xA;This is my quick recipe for a basic bash logging solution with line-based truncation: the logfile will never exceed RETAIN\NUM\LINES lines (actually, it will never exceed such size when the script is launched, so the number of lines when the script has run is RETAIN\NUM\LINES + number of lines logged by the script).&#xA;&#xA;Whenever you use the log function you&#39;ll get output on stdout as well, complete with a timestamp.&#xA;&#xA;!/bin/bash&#xA;&#xA;set LOGFILE to the full path of your desired logfile; make sure&#xA;you have write permissions there. set RETAINNUMLINES to the&#xA;maximum number of lines that should be retained at the beginning&#xA;of your program execution.&#xA;execute &#39;logsetup&#39; once at the beginning of your script, then&#xA;use &#39;log&#39; how many times you like.&#xA;&#xA;LOGFILE=quicklog.sh.log&#xA;RETAINNUMLINES=10&#xA;&#xA;function logsetup {&#xA;    TMP=$(tail -n $RETAINNUM_LINES $LOGFILE 2  /dev/null) &amp;&amp; echo &#34;${TMP}&#34;   $LOGFILE&#xA;    exec     (tee -a $LOGFILE)&#xA;    exec 2  &amp;1&#xA;}&#xA;&#xA;function log {&#xA;    echo &#34;[$(date --rfc-3339=seconds)]: $&#34;&#xA;}&#xA;&#xA;logsetup&#xA;&#xA;log hello this is a log&#xA;&#xA;Launch this a lot of times and you&#39;ll end up with this in quicklog.sh.log*:&#xA;&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:10+02:00]: hello this is a log&#xA;[2016-09-26 11:44:10+02:00]: hello this is a log&#xA;[2016-09-26 11:44:10+02:00]: hello this is a log&#xA;[2016-09-26 11:44:11+02:00]: hello this is a log&#xA;[2016-09-26 11:44:11+02:00]: hello this is a log&#xA;[2016-09-26 11:44:11+02:00]: hello this is a log&#xA;[2016-09-26 11:44:14+02:00]: hello this is a log&#xA;&#xA;Enjoy!&#xA;&#xA;UPDATED on September 26th, 2016 to employ the rfc-3339 date option, which makes the logline quite more readable and greppable.&#xA;&#xA;#MostlyUnixish #bash #scripting #shell&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Sometimes you need to write a quick shell script; many a time, you need to put it on a server, and launch it every now and then, or run it in a cron job. That script may end up doing something important and/or altering the state of the server machine.</p>

<p>If that script starts being using often enough, you&#39;ll want logging as well, and that can be a complex beast to tame, for logs require rotation and deletion, otherwise the beast will eat up
all your disk space. Setting up <a href="http://linuxcommand.org/man_pages/logrotate8.html" rel="nofollow">logrotate</a> for a single shell script looks like an overkill to me most of the times.</p>

<p>So what? Many a times logging will be neglected, and you&#39;ll lose the ability to track what happened and why.</p>

<p>This is my quick recipe for a basic bash logging solution with line-based truncation: the logfile will never exceed <strong>RETAIN_NUM_LINES</strong> lines (actually, it will never exceed such size when the script is launched, so the number of lines when the script has run is RETAIN_NUM_LINES + number of lines logged by the script).</p>

<p>Whenever you use the <em>log</em> function you&#39;ll get output on stdout as well, complete with a timestamp.</p>

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

# set LOGFILE to the full path of your desired logfile; make sure
# you have write permissions there. set RETAIN_NUM_LINES to the
# maximum number of lines that should be retained at the beginning
# of your program execution.
# execute &#39;logsetup&#39; once at the beginning of your script, then
# use &#39;log&#39; how many times you like.

LOGFILE=quicklog.sh.log
RETAIN_NUM_LINES=10

function logsetup {
    TMP=$(tail -n $RETAIN_NUM_LINES $LOGFILE 2&gt;/dev/null) &amp;&amp; echo &#34;${TMP}&#34; &gt; $LOGFILE
    exec &gt; &gt;(tee -a $LOGFILE)
    exec 2&gt;&amp;1
}

function log {
    echo &#34;[$(date --rfc-3339=seconds)]: $*&#34;
}

logsetup

log hello this is a log
</code></pre>

<p>Launch this a lot of times and you&#39;ll end up with this in <em>quicklog.sh.log</em>:</p>

<pre><code>[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:10+02:00]: hello this is a log
[2016-09-26 11:44:10+02:00]: hello this is a log
[2016-09-26 11:44:10+02:00]: hello this is a log
[2016-09-26 11:44:11+02:00]: hello this is a log
[2016-09-26 11:44:11+02:00]: hello this is a log
[2016-09-26 11:44:11+02:00]: hello this is a log
[2016-09-26 11:44:14+02:00]: hello this is a log
</code></pre>

<p>Enjoy!</p>

<p><strong>UPDATED</strong> on September 26th, 2016 to employ the rfc-3339 date option, which makes the logline quite more readable and greppable.</p>

<p><a href="/alan/tag:MostlyUnixish" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">MostlyUnixish</span></a> <a href="/alan/tag:bash" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">bash</span></a> <a href="/alan/tag:scripting" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">scripting</span></a> <a href="/alan/tag:shell" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">shell</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/quick-log-for-bash-scripts-with-line-limit</guid>
      <pubDate>Tue, 08 Oct 2013 20:47:00 +0000</pubDate>
    </item>
  </channel>
</rss>