I have a task where I am reading a large file(~ 2 GB) and the file is getting appended every few seconds and have a cron job set that runs every 5 mins. Now this cron job checks and gets only the "appended" lines and looks for a pattern in those appended lines and stores the lines containing that pattern in some kind of file/data structure.
now my idea around this is
- check for the file and see if it has changed since last run, if "yes" get what lines have been appended to it.
- Look for the pattern in the appended lines and get the lines and store them somewhere. I am clear on the code in step2, but I'm stuck in step1.
Since the file is appending, how do I look for lines that are appended.
Can I use something like "watch -n 5 mylog.txt" and run this via subprocess module and see what lines have changed or is there any other good way to do it.
OR
see what was the line number of the last line from last Run and check the line number in the next run and print lines between these 2, but how do I put this to code. I have no clue.
Please someone help.