Results 1 to 9 of 9

Thread: bash script for displaying a log file

  1. #1

    Default bash script for displaying a log file

    An email server rotates its log files every night at midnight, using the data pattern yyyy-mm-dd.log. I wish to display the log file using tail in a terminal window.

    The problem is that "tail --follow <log_file>" does not terminate when the log file is changed. The old log file ceases to receive more log entries. I tried "--max-unchanged-stats" hoping that tail would terminate; no luck there.

    Does anyone have a method that would cause tail to end allowing the next log file for display?

    Code:
    logfile="`date +%Y-%m-%d`.log"
    while [ -f $logfile  ]
    do
      echo Opening log file $logfile
      tail --max-unchanged-stats=10 -f $logfile
      logfile="`date +%Y-%m-%d`.log"
    done

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,588

    Default Re: bash script for displaying a log file

    When I read
    Code:
    man tail
    I see
    --max-unchanged-stats=N
    with --follow=name, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files). With inotify, this option is rarely useful.
    and you do not have the --follow=name .
    Henk van Velden

  3. #3
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,538
    Blog Entries
    15

    Default Re: bash script for displaying a log file

    Hi
    If it's postfix you can add your user to the systemd-journal group and just follow the postfix user journal entries?

    Perhaps you could clarify what your looking for in the logs, there are programs like sec (simple event correlator) that can be configured for certain events or groups of events.
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  4. #4

    Default Re: bash script for displaying a log file

    and you do not have the --follow=name
    Okay, I changed that. I will not know if it works until midnight.

    If it's postfix
    No, it is not. It is Communigate mail server. It does not use a common file name for the current log file. Rather it creates a new log file at midnight with the new day's date.

    I am not looking for anything in particular. I have found that having the recent output of log files helps with identifying problems.

  5. #5

    Default Re: bash script for displaying a log file

    Quote Originally Posted by jimoe666 View Post
    and you do not have the --follow=name
    Okay, I changed that. I will not know if it works until midnight.
    It did not change anything. When tail decides the file is inactive, it outputs "*END*" and just sits there, no exit.

    Does anyone have a method that would cause tail to end allowing the next log file for display?

  6. #6
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,538
    Blog Entries
    15

    Default Re: bash script for displaying a log file

    On Wed 25 Jul 2018 05:26:03 PM CDT, jimoe666 wrote:

    jimoe666;2874907 Wrote:
    > > > > and you do not have the --follow=-name-> >

    > Okay, I changed that. I will not know if it works until midnight.

    It did not change anything. When tail decides the file is inactive, it
    outputs "*END*" and just sits there, no exit.

    Does anyone have a method that would cause tail to end allowing the next
    log file for display?


    Hi
    Send a signal (kill -l ) to the process, probably sighup.

    --
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SLES 15 | GNOME Shell 3.26.2 | 4.12.14-23-default
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!


  7. #7

    Default Re: bash script for displaying a log file

    Quote Originally Posted by malcolmlewis View Post
    Hi
    Send a signal (kill -l ) to the process, probably sighup.
    I considered that.
    There are multiple instances of tail running, so "pidof" is not helpful.

    How do I get the process's PID?

  8. #8
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,538
    Blog Entries
    15

    Default Re: bash script for displaying a log file

    On Wed 25 Jul 2018 06:16:03 PM CDT, jimoe666 wrote:

    malcolmlewis;2875091 Wrote:
    > Hi
    > Send a signal (kill -l ) to the process, probably sighup.

    I considered that.
    There are multiple instances of tail running, so "pidof" is not helpful.

    How do I get the process's PID?
    Hi
    In your script when it starts... echo $! should get you what you
    need. I normally use a file test to check it exists, then on shutdown
    remove it. So use a case "$1" in start) and stop).

    Even easier these days with a systemd service (it knows the pid) and
    timer to run at the rollover time....

    --
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SLES 15 | GNOME Shell 3.26.2 | 4.12.14-23-default
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!


  9. #9

    Default Re: bash script for displaying a log file

    This is what I have working to change the target logfile name. If there is a better way to wait for midnight to end the current "tail" process, let me know.

    Code:
      logfile="$(date +%Y-%m-%d).log"
      while [ -f $logfile  ]
      do
        echo Opening log file $logfile
    
        txt="tail --max-unchanged-stats=10 --follow=name $logfile"
        $txt &
    
        pid=$!
        echo "$txt. pid [$pid]"
    
        while true
        do
          time1=$(date +%H:%M:%S)
          if [ "$time1" = "00:00:01" ]
          then
            kill $pid
            break;
          fi
          sleep 1;
        done
    
        logfile="$(date +%Y-%m-%d).log"
      done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •