Link to home
Start Free TrialLog in
Avatar of joaotelles
joaotellesFlag for United States of America

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.
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
}

Open in new window

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;
}

Open in new window


Any suggestion are very welcome.

Thanks!
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joaotelles

ASKER

sleep 60
Ok, I will try.

I will also test this,

for file in $directory
do
	if [ "${file}" == "main_filename.txt" ]
	then
		move_files;
  		echo "Main File Processed!"
  		echo "Files moved"
		break
	fi
done

Open in new window

Avatar of skullnobrains
skullnobrains

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)
for file in $directory
do
	if [ "${file}" == "main_filename.txt" ]
	then
		move_files;
  		echo "Main File Processed!"
  		echo "Files moved"
		break
	fi
done

Open in new window


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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial