0

How to get last changes in text file on Linux? Example:

text1
text2
text3

and then a programme adds another line to the text file:

demo1

If the last change was the addition of demo1, print in console:

while command; do
if tail -f -n3 "out.log" | grep -q "demo1" ; then
    echo "Application started with success."
else
    echo "Startup failed."
fi
done

The problem is that *demo1* can be repeated multiple times in the log file and this code constantly duplicates all the results of a file change. I need the word demo1 to appear in real time in the log, then a message is displayed. And so every time a word appears.

in text i understeand it:

look last update -> if i see demo1 -> i print text Application started with success. -> look last update again -> if i see demo1 -> i print text Application started with success. -> look last update -> if i dont see demo1 -> i print text Startup failed. -> look last update -> if i see demo1 -> i print text Application started with success............

but how to display it in code?

example log file:

text1
text2
text3
demo1
text1
text2
text3
demo1
text1
text2
text3
  • 1
    So you want only one alert when "demo1" appears? For what time period or amount of repetition? – Jeff Schaller Jul 9 at 13:41
  • You should edit your question and add more background information about what you want to achieve and the conditions/constraints. Checking the log file doesn't seem to be the best solution. Is command the only program that writes to out.log? Can there be more than one running instance of this program? Is it a program you wrote yourself? Or a commonly used application? (Which one?) Your question may be an XY problem, see meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Bodo Jul 9 at 15:11
  • (1) Can you keep a backup copy of the file, periodically compare it to the current file, and look at the change(s), if any?  (2) I agree that you should provide more information.  Is command putting itself into the background, or starting a background process?  Is the exit status of the command command important?  Why are you running it in a while loop? (3) Please indent your code. – G-Man Jul 9 at 19:23
  • I still don’t understand your question.  Do you run command multiple times?  For the output you added to the question, do you want to say “Startup failed.” nine times and “Application started with success.” twice?  And please answer the other questions we asked you. – G-Man Jul 10 at 1:46
  • So, have you lost interest?  I am developing a solution that might be what you want, but I’m not going to go to the effort of writing it up and posting it unless I get a clearer indication that it is what you want. – G-Man Jul 10 at 18:29
0

I’m taking a stab in the gloom, since I don’t entirely understand the question, but try

(sleep 1; command || printf 'command failed with exit code %s.\n' "$?") &
if tail -f –n0 "out.log" | grep -q "demo1"
then
    echo "Application started with success."
else
    echo "Startup failed."
fi
  • tail -n0 will output the last 0 (zero) lines of the file.  By itself, this is fairly useless.  In combination with -f, it lets you look at only the additions to the file and not any of the current content.
  • I don’t know the timing / dynamics of command and writing to the log file.  I’m speculating that demo1 might get written to the file very quickly; so fast that it might already be there before you execute the tail command in your script.  I’m guessing that that’s why you use -n3, so you can look at lines that might have been added in the last microsecond.

    By delaying the execution of command (with sleep), we start tail -f running before command starts, so we should capture everything that gets written to the file now.

    If your version of sleep accepts non-integer arguments, you can probably get away with sleeping for less than one second.

  • AFAICT, this should work.  But I’m having trouble getting it to work; it seems like my shell (which is bash) isn’t considering the tail … | grep … pipeline to be done until both the grep and the tail have exited.  This kludge tentatively seems to fix that:
    (sleep 1; command || printf 'command failed with exit code %s.\n' "$?") &
    if grep -q "demo1" <(tail -f –n0 "out.log")
    then
        echo "Application started with success."
    else
        echo "Startup failed."
    fi
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.