Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7798559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:02:53+00:00 2026-06-02T00:02:53+00:00

I am trying to read a constantly changing log file (that is text file)

  • 0

I am trying to read a constantly changing log file (that is text file) and generate an alert on some text found in that file. I want to do it through bash scripting but I’m confused that how i ll invoke my program as soon as some text comes into that log file.

In other words, how can i keep a check all the time on that file so that whenever some specific text comes into that file, i get alert regarding that text. any function call? or something else? Please advise.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:02:54+00:00Added an answer on June 2, 2026 at 12:02 am

    the way i think i would go about it is this:

    first of all i will create a bash script and a text file, the bash script will first store the number of lines of that log file in the text file, then add a cron of that script.

    the script will count the lines of that file and compare it to the number of lines stored in the text file

    you can do that using the command

    wc -l filename
    

    if the number of lines is bigger then the log has been updated, i will calculate the current number of lines minus the number stored in the text field, and by that i will know how many lines have been added since last check, then use this command to fetch those lines

    tail -n $number_of_lines filename
    

    and i will loop through the output and do the desired action if one of the lines matches your criteria and then update the text file which holds the number of lines, i would have written you the bash script but i guess you got the idea

    EDIT: you can execute this script (just write your code which searches for the matches etc)

    #!/bin/bash
    
    NUMBER=`/bin/cat ./numberoflines`
    LINES=`/usr/bin/wc -l < /var/log/messages`
    
    DIFFERENCE=$(($LINES-$NUMBER))
    
    if [ $DIFFERENCE != 0 ]; then
    tail -n $DIFFERENCE /var/log/messages |while read line
    do
    if $( echo $line | grep --quiet 'New USB device found' )
    then
            # Email text/message
            EMAIL='youremail@gmail.com'
            SUBJECT="New USB device found"
            EMAILMESSAGE="/tmp/emailmessage.txt"
            echo "New USB device found etc ..." > $EMAILMESSAGE
            /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
    fi
    done
    echo "$LINES" > ./numberoflines
    fi
    

    EVEN if you cannot add anything to crons then you still can execute this script anyway, see what you want to match and check and execute the code you need there

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read the text content of a pdf file into a
I'm trying to read a text file (Using BufferedReader and FileReader) from my file
I am trying to read a file type that has a mixture of integers,
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =
Iam trying to read a binary file to memory and pass the starting address
I'm trying to read some data on the Serial Port but I doesn't have
I'm trying to read a TIFF image from file using BufferedImage. The following is
I am trying to make an script that will every second read string from
I'm trying to read a CSV file generated by M$ Excel on linux. The
Read the edit below for more information. I have some code below that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.