Hello,

I need some help with the following script:

if ! [ -f ${APATH}/myfile.txt ];then
  echo $(date +%Y%m%d"_"%H%M%S)": Nu am gasit fisierul ${APATH}/myfile.txt"
  ps -fxu pin | grep "/usr/local/coreutils/bin/tail -f ${LOG_PATH}/x.log$" | awk '{system("kill "$2)}'
  cat ${LOG_PATH}/x.log | sed -n -e '/LONG/{x;1!p;g;;}' -e h > ${APATH}/myfile.txt
  
  /usr/local/coreutils/bin/tail -f ${LOG_PATH}/x.log | sed -n -e '/LONG/{x;1!p;g;;}' -e h >> ${APATH}/myfile.txt &
fi
date1=$(tail -1 ${PATH}/myfile.txt | cut -d" " -f2-5)
if [ -f exec_long.date ];then
  date2=$(cat exec_long.date)
else
  date2=$date1
  echo $date1 > exec_long.date
fi
if ! [ "$date1" = "$date2" ];then
        ${CAL_PATH}/${APP_NAME}
        sleep 1
        echo $date1 > exec_long.date
fi

I have a continuously growing log file (x.log) in which i have to look for certain lines that contain "Long". The line above each line containing the word "Long" it contains a time stamp. I want to extract each line containing the time stamp into myfile.txt and check the difference between time stamps. Whenever there is a difference i need to run another script (${CAL_PATH}/${APP_NAME}), then sleep 1, then continue searching.

Lines with "Long" do not appear continuously, but in blasts. The script runs fine until the first pause encountered. Starting with the first pause, tail -f doesn't write in myfile.txt anymore.

Can someone help me understand why "tail -f" it stops writing into myfile.txt? Or does someone know an alternative to "tail -f" of achieving the initial scope of the script?

I'll appreciate any help.
Thanks!

you can do a

tail-1000f <your-log-file>

This command will show the latest 1000 lines from your log file...also if you log file is updated, it will only show the last 1000 lines...

>> Can someone help me understand why "tail -f" it stops writing into myfile.txt

Does the log file get rotated at some point? If so, tail is still referencing the OLD version, even though it's been renamed.

I've done something similar using tail -f, but also running a monitoring script that checks the log file to see if it's been rotated (I check logfile.txt every so often to see if the inode has changed)

If the file gets rotated, the monitor script kills the tail script and re-starts it.

As far as alternatives, I"m sure there is one, but I can't think of one at the moment using bash...

I hope this helps!

-G

This article has been dead for over six months. Start a new discussion instead.