While loop in certain interval

Hi Gurus,

I have a script which loops a directory to find the file. right now, I want to add time interval which means every 15 minutes it checks the dir once. if all files are found, then exit with 0. if there are some files missing at 6:00am the process exit with 20. the script start at 7:00pm everyday by third part scheduler.
My current script like:

IFS=","
while read group file X
do   
   if [ -f $file ] ; then 
      time=$(ls -l $file |awk '{print $6,$7,$8}')
      echo "$group, $file, $time, YES"
   else 
      echo "$group, $file , NO"
   fi
done < list.txt

Thanks in advance.

Use crontab

*/15 * * * * /path/to/command

cron - Wikipedia, the free encyclopedia

1 Like

Thanks for your reply,

unfortunately, I can not use CRONTAB, I have to add this in my script.

I think you can tell system to sleep for 900 seconds with infinite loop..might others will have better solution

---------- Post updated at 11:28 AM ---------- Previous update was at 11:23 AM ----------

something like this

$ while :; do sleep 1;echo "1 sec over start script again"; done
1 Like

Thanks for your reply.

This is I want, but I don't know how to implement this into my existing code. would you please give me a sample. (I need sleep 900 seconds and checking if the time is 6:00 AM or after 6:00AM, if yes, the script stops checking)
I tired below code: but I don't know how to check if the time is 6:00AM or later. would you please give me some help.

while :
do
time1=`date +"%d-%m-%Y:%T"`
echo "Start time: " $time1
sleep 60
time2=`date +"%d-%m-%Y:%T"`
echo "End time: " $time2
done

Thanks in advance

try

while :
do 
   
IFS=","
while read group file X
do   
   if [ -f $file ] ; then 
      time=$(ls -l $file |awk '{print $6,$7,$8}')
      echo "$group, $file, $time, YES"
   else 
      echo "$group, $file , NO"
   fi
done < list.txt

    sleep 900
    echo " 900 sec over start script again"

done
1 Like

Thanks for you quick reply.
I have one more question: how could I check if current time is 6:00AM over or not, because I need exits with error code after 6:00AM, if there are still some file missing.

Thanks in advance

try something like this

$ now=$(date +%s)
$ a_6am_cross=$(date -d" 6:00:01 AM" +%s)
$ if [ $now -eq $a_6am_cross ]; then echo "Its 6:00:01 exiting your exit code goes here"; else echo "Keep looping";fi
1 Like

Thanks,

when implement this code, I got below error:
$ a_6am_cross=$(date -d" 6:00:01 AM" +%s)

date: illegal option -- d
date: illegal option --
date: illegal option -- 6
date: illegal option -- :
date: illegal option -- 0
date: illegal option -- 0
date: illegal option -- :
date: illegal option -- 0
date: illegal option -- 1
date: illegal option --
date: illegal option -- A
date: illegal option -- M

Would you please take a look.

Thanks

Its working on my pc...which OS ?

a_6am_cross=$(date -d " 6:00:01 AM" +%s)

try simply these commands on terminal first

$ date -d " 6:00:01 AM" +%s
$ date +%s

1 Like

I assume the error lies in communication :wink:

Dont have to copy paste the leading $ (or # in other examples).
$ indicates you write the command as user.
# indicates you write the command as root.

That comes from the indicator of PS1 PS1="\\$"
Which shows either $ or # depending on user ID (eg: 0 vs 500/1000).

Hope this helps

EDIT: Err, just tried myself, doesnt seem to be the reason - sorry.
Might be of shopt setting maybe?
Works well on 3.11.3-201.fc19.x86_64.

1 Like

when running

date +%s

I got

%s

when running

date -d " 6:00:01 AM" +%s

I got error:

date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

My OS is SunOS Generic_144488-17 sun4v sparc SUNW,SPARC-Enterprise.

Would you please take a look.

man watch
1 Like

Not sure if all prerequisites are met by your system ( stat , awk and date commands available and correct version), but this might be worth a shot:

mkfifo goodfiles     
while [ $(date +%H) -ge 19 -o $(date +%H) -lt 6 ]
  do  awk '{print $2 "\tYES"}' goodfiles & LC_ALL=C stat -c"%G %n" $(cut -d" " -f2 files.txt) 2>&1 1>goodfiles | awk -F\' '{print $2 "\tNO"} END {if (NR>0) exit 20}'; 
      sleep 900
  done

Output of that (yes admittedly, too) long line would be e.g.

file    YES
file~    YES
file1    YES
file1~    YES
file2    YES
file2~    YES
file3    YES
file3~    YES
file4    YES
file4~    YES
file6    NO
file5    NO

, and exit status 20 should files be missing.

1 Like

I think RudiC's long line may have been truncated.

I also think the following may do what you want on a SunOS system, but since you didn't supply any sample of your list.txt file contents, I can't make any guarantees.

#!/bin/ksh
while hour=$(date +%H)
do      if [ $hour -ge 6 ] && [ $hour -lt 19 ]
        then    exit 20
        fi
        all_found=1
        while IFS="," read group file X
        do      if [ -f "$file" ]
                then    time=$(ls -l "$file" | nawk '{print $6,$7,$8}')
                        printf "%s,%s,%s,YES\n" "$group" "$file" "$time"
                else    all_found=0
                        printf "%s,%s,NO\n" "$group" "$file" 
                fi
        done < list.txt
        if [ $all_found -eq 1 ]
        then    exit 0
        else    sleep 900
        fi
done
2 Likes

Thanks. Yes, it was truncated, I corrected the post.