joaotelles
asked on
Bash - Loop function with specific period of time
I have a working function that checks if a file exists in a specific directory then calls another function that moves some other files, if the file does not exist, the functions calls itself. I cant calls the move files function before check if the main file exist in the proper directory.
I am afraid this loop might crash if it gets too long. Its working because its not planned to get too long, but would be better avoid this risk.
So, instead of calling the same funtion, I should call something else, Maybe calling a function TIME (just a draft),
Any suggestion are very welcome.
Thanks!
function WORK {
cd $directory
FILENAME='main_filename_$var.txt'
if [ -f $FILENAME ]; then
move_files; #calls another function
echo "Main File Processed"
echo "Files Moved"
else
echo "Main file wasnt processed yet..."
echo "Waiting..." # should wait before continous
WORK;
fi
}
But I would like to use a better function, I am trying to find something that would precisely wait for some period (e.g one minute) and then calls the function again. I am afraid this loop might crash if it gets too long. Its working because its not planned to get too long, but would be better avoid this risk.
So, instead of calling the same funtion, I should call something else, Maybe calling a function TIME (just a draft),
function TIME {
minute=$(date +"%M")
one_more=`$minute + 1`
while [ `$(date +"%M")` =! `$one_more` ]
do
echo "Waiting..."
done
WORK;
}
Any suggestion are very welcome.
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can also cron your script every minute and let it just die when it has nothing to do (when the master file is not there)
----
you can also use inotifywait to wait until the file is created
something like this might do
inotifywait -m -e moved_to $directory | fgrep main_filename.txt | while read line
do move_files
done
inotifywait will wait for files to be moved to $directory and print a line for each moved file
grep makes sure a line is output if and only if the filename matches (with a lazy ereg)
----
you can also use inotifywait to wait until the file is created
something like this might do
inotifywait -m -e moved_to $directory | fgrep main_filename.txt | while read line
do move_files
done
inotifywait will wait for files to be moved to $directory and print a line for each moved file
grep makes sure a line is output if and only if the filename matches (with a lazy ereg)
ASKER
for file in $directory
do
if [ "${file}" == "main_filename.txt" ]
then
move_files;
echo "Main File Processed!"
echo "Files moved"
break
fi
done
It does not work, it looks for every file in the directory and if the file isnt found it ends whe loop.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I will also test this,
Open in new window