Is this way of finding the time difference a correct way of doing it or is it error-prone.
#****Check if lastrun exist. If exists check if the difference is 1 hour or not and act accordingly***********************
if [ -f $LOG_DIR/lastrun ]; then
lastrun_time=`cat $LOG_DIR/lastrun`
curr_time=`date +%s`
#Here we are checking if the last run was within the last hour, if true we simply exit without running.
if [ $((curr_time-lastrun_time)) -le 3600 ]; then
#echo "File is being run within an hour of last run.exiting."
exit
else
#Removing last run, if the last run is beyond an hour we remove the file and rerun the grep.
rm -f LOG_DIR/lastrun
fi
fi
TIA
Sreekanth
$(($curr_time - $lastrun_time))
Please use code tags.
What does $LOG_DIR/lastrun
contain ?
--
Actually, POSIX compliant shells should not require dollar signs when using integers in an arithmetic expansion:
Shell Command Language: 2.6.4 Arithmetic Expansion
1 Like
Scutinizer,
lastrun contains the time when the script was last run, the same as `date +%s`
Tks,
Sreekanth
If it contains epoch time then I would think this is a valid way of finding the difference.
I noticed a $-sign is missing
rm -f LOG_DIR/lastrun
It is a good practice to use double quotes around variable reference:
rm -f "$LOG_DIR/lastrun"
Although I am sure the path to your log dir will not contain spaces or funny characters..
You might like to use something like:
exit 1
So you have a return code..
1 Like