Java IO implementation of unix/linux “tail -f”
Take a look at Apache Commons implementation of Tailer class. It does seem to handle log rotation as well.
Take a look at Apache Commons implementation of Tailer class. It does seem to handle log rotation as well.
Non Blocking If you are on linux (as windows does not support calling select on files) you can use the subprocess module along with the select module. import time import subprocess import select f = subprocess.Popen([‘tail’,’-F’,filename],\ stdout=subprocess.PIPE,stderr=subprocess.PIPE) p = select.poll() p.register(f.stdout) while True: if p.poll(1): print f.stdout.readline() time.sleep(1) This polls the output pipe for new … Read more
In a cluster best practices are to gather all logs in a single point through an aggregator and analyze them with a dedicated tool. For that reason in K8S, log command is quite basic. Anyway kubectl logs -h shows some options useful for you: # Display only the most recent 20 lines of output in … Read more
Under Python 3.x, you can do this nicely: >>> head, *tail = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >>> head 1 >>> tail [1, 2, 3, 5, 8, 13, 21, 34, 55] A new feature in 3.x is to use the * operator in unpacking, to mean any extra values. It … Read more
tail –help gives the following: -n, –lines=K output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth So to filter out the first 2 lines, -n +3 should give you the output you are looking for (start from 3rd).
You don’t see anything, because of buffering. The output is shown, when there are enough lines or end of file is reached. tail -f means wait for more input, but there are no more lines in file and so the pipe to grep is never closed. If you omit -f from tail the output is … Read more
This may be quicker than yours. Makes no assumptions about line length. Backs through the file one block at a time till it’s found the right number of ‘\n’ characters. def tail( f, lines=20 ): total_lines_wanted = lines BLOCK_SIZE = 1024 f.seek(0, 2) block_end_byte = f.tell() lines_to_go = total_lines_wanted block_number = -1 blocks = [] … Read more
Use the -wait parameter with Get-Content, which displays lines as they are added to the file. This feature was present in PowerShell v1, but for some reason not documented well in v2. Here is an example Get-Content -Path “C:\scripts\test.txt” -Wait Once you run this, update and save the file and you will see the changes … Read more
If you use PowerShell then this works: Get-Content filenamehere -Wait -Tail 30 Posting Stefan’s comment from below, so people don’t miss it PowerShell 3 introduces a -Tail parameter to include only the last x lines
Turn on grep‘s line buffering mode when using BSD grep (FreeBSD, Mac OS X etc.) tail -f file | grep –line-buffered my_pattern It looks like a while ago –line-buffered didn’t matter for GNU grep (used on pretty much any Linux) as it flushed by default (YMMV for other Unix-likes such as SmartOS, AIX or QNX). … Read more