brest1007 brest1007 - 4 months ago 12
Bash Question

Shell: check if no new line is added to file for 5 seconds

I have a script that writes new lines to a local file called "log" line by line, I want to know when it stops writing to file.

I want to write another script to check if no new line is added to file "log" for 5 seconds, then I print on screen "finished".

How can I do this in a loop?

Answer

You can use bash word count tool, wc with flag for reading lines twice to achieve this:-

#!/bin/bash

initial=$(wc -l < log)     # first capture of the number of lines
sleep 5s                    # Sleep for 5s

later=$(wc -l < log)       # Second capture of the line count

((later - initial)) && echo "Writing to log pending" || echo "Writing to log finished"
Comments