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 &
date1=$(tail -1 ${PATH}/myfile.txt | cut -d" " -f2-5)
if [ -f exec_long.date ];then
  date2=$(cat exec_long.date)
  echo $date1 > exec_long.date
if ! [ "$date1" = "$date2" ];then
        sleep 1
        echo $date1 > exec_long.date

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.

Recommended Answers

All 2 Replies

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!


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.