CyberAngel
August 21st, 2008, 10:43 AM
Hello,
I have a situation that I need to be monitoring a file using "tail -f" for changes.
The filename will always be something like this:
YYYYMMDD.dat
for example 20080821.dat
When the day is changing, a new file is created with the new date for it`s filename. So if today the file is 20080821.dat the next one will be 20080822.dat
My problem now, is that when that occurs (new file just created) I want to stop monitoring the old one and instantly start monitoring the new one.
How do you think it`s the best way to achieve that?
I have a bash script that it is checking the file like this:
#!/bin/bash
tail -f $1 | while read line; do
#DO SOMETHING
done
and I can also know which is the last filename using a polling technique like this:
#!/bin/bash
while [ 1 ]; do
LATEST_FILE=`ls | grep '20[0-9][0-9]\(0[1-9]\|1[0-2]\)\(0[1-9]\|[1-2][0-9]\|3[0-1]\).dat' | tail -1`
sleep 1
done
Both of these code blocks has an endless loop so I can`t combine them in a single script without the use of threads.
Is it possible to use threads for bash scripts and call from the second Code block the first one like a function, when I determine that the $LATEST_FILE just changed?
Thanks
I have a situation that I need to be monitoring a file using "tail -f" for changes.
The filename will always be something like this:
YYYYMMDD.dat
for example 20080821.dat
When the day is changing, a new file is created with the new date for it`s filename. So if today the file is 20080821.dat the next one will be 20080822.dat
My problem now, is that when that occurs (new file just created) I want to stop monitoring the old one and instantly start monitoring the new one.
How do you think it`s the best way to achieve that?
I have a bash script that it is checking the file like this:
#!/bin/bash
tail -f $1 | while read line; do
#DO SOMETHING
done
and I can also know which is the last filename using a polling technique like this:
#!/bin/bash
while [ 1 ]; do
LATEST_FILE=`ls | grep '20[0-9][0-9]\(0[1-9]\|1[0-2]\)\(0[1-9]\|[1-2][0-9]\|3[0-1]\).dat' | tail -1`
sleep 1
done
Both of these code blocks has an endless loop so I can`t combine them in a single script without the use of threads.
Is it possible to use threads for bash scripts and call from the second Code block the first one like a function, when I determine that the $LATEST_FILE just changed?
Thanks