Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, here's an idea, inspired by <a href="http://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Files_and_streams" rel="nofollow noreferrer">http://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Files_and_streams</a></p> <ol> <li><p>make a named pipe:</p> <pre><code>mkfifo /dev/mypipe </code></pre></li> <li><p>redirect stdout and stderr to the named pipe:</p> <pre><code>&amp;&gt; /dev/mypipe </code></pre></li> <li><p>read from mypipe into a file:</p> <pre><code>cat &lt; /dev/mypipe &gt; /var/log/log.txt &amp; </code></pre></li> <li><p>when you need to log-rotate, kill the cat, rotate the log, and restart the cat.</p></li> </ol> <p>Now, I haven't tested this. Tell us how it goes.</p> <p>Note: you can give the named pipe any name, like /var/tmp/pipe1 , /var/log/pipe , /tmp/abracadabra , and so on. Just make sure to re-create the pipe after booting <em>before</em> your logging-script runs.</p> <hr> <p>Alternatively, don't use cat, but use a simple script file:</p> <pre><code>#!/bin/bash while : ; do read line printf "%s\n" "$line" done </code></pre> <p>This script guarantees an output for every newline read. (cat might not start outputting until its buffer is full or it encounters an EOF)</p> <hr> <h2>Final -- and TESTED -- attempt</h2> <p><strong><em>IMPORTANT NOTE:</strong> Please read the comments from <strong>@andrew</strong> below. There are several situations which you need to be aware of.</em></p> <p>Alright! Finally got access to my Linux box. Here's how:</p> <p><strong>Step 1:</strong> Make this recorder script:</p> <pre><code>#!/bin/bash LOGFILE="/path/to/log/file" SEMAPHORE="/path/to/log/file.semaphore" while : ; do read line while [[ -f $SEMAPHORE ]]; do sleep 1s done printf "%s\n" "$line" &gt;&gt; $LOGFILE done </code></pre> <p><strong>Step 2:</strong> put the recorder into work:</p> <ol> <li><p>Make a named pipe:</p> <pre><code>mkfifo $PIPENAME </code></pre></li> <li><p>Redirect your application's STDOUT &amp; STDERR to the named pipe:</p> <pre><code>...things... &amp;&gt; $PIPENAME </code></pre></li> <li><p>Start the recorder:</p> <pre><code>/path/to/recorder.sh &lt; $PIPENAME &amp; </code></pre> <p>You might want to <code>nohup</code> the above to make it survive logouts.</p></li> <li><p>Done!</p></li> </ol> <p><strong>Step 3:</strong> If you need to logrotate, pause the recorder:</p> <pre><code>touch /path/to/log/file.semaphore mv /path/to/log/file /path/to/archive/of/log/file rm /path/to/log/file.semaphore </code></pre> <p>I suggest putting the above steps into its own script. Feel free to change the 2nd line to whatever log-rotating method you want to use.</p> <hr> <p><strong><em>Note :</em></strong> If you're handy with C programming, you might want to make a short C program to perform the function of <code>recorder.sh</code>. Compiled C programs will certainly be lighter than a nohup-ed detached bash script.</p> <hr> <p><strong><em>Note 2:</em></strong> David Newcomb provided a helpful warning in the comments: <em>While the recorder is not running then writes to the pipe will block and</em> may <em>cause the program to fail unpredictably. Make sure the recorder is down (or rotating) for as short time as possible.</em></p> <p>So, if you can ensure that rotating happens <em>really</em> quickly, you can replace <code>sleep</code> (a built-in command which accepts only integer values) with <code>/bin/sleep</code> (a program that accepts float values) and set the sleep period to <code>0.5</code> or shorter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload