Sponsored Content
Top Forums Shell Programming and Scripting KSH - Issue with endless loop. Post 302532691 by Sylvan303 on Tuesday 21st of June 2011 07:39:25 PM
Old 06-21-2011
KSH - Issue with endless loop.

First time post. I did a search so I didn’t see this specific issue. It seems to be a head scratcher for me.
I have an hourly job that on rare occasions, gets into an endless loop.
I’ve tried different scenarios but the current version does basically the following.
Find all the *.arc files and copy them to a new location. Since this runs every hour, I only copy files that don’t already exist in the new location. There is another process which follows the same logic and deletes from both the source as well as the destination. The only difference is I use the +mtime in the find command because I keep a weeks worth of files on the source and 10 days on the destination.
Every once in a while ( 3 to 4 times in the last 6 months), this script gets into an endless loop and the output fills up the log directory. We have to kill the process and cleanup the files.
Three questions:
1) Any thoughts on what I’m doing would cause an endless loop?
2) Any change in logic that might prevent this problem?
3) Any way to insert a trace to help trap the cause?
Here is the KSH function that does the copy:
Code:
# ------------------------------------------------------------------------------
# Copy archive files to the backup locaton. Skip the most current archive file.
# ------------------------------------------------------------------------------
save_files() {
ERR=N

   sav_files1="/tmp/save_files1_${DB}.tmp"
   sav_log1="/tmp/save_log1_${DB}.tmp"

   >$sav_files1
   >$sav_log1

  find ${dir_dest} -type f -name \*.arc > $archive_list
  curr_nbr=$(cat $archive_list | wc -l)

  if [[ $curr_nbr -gt 1 ]]; then
     #skip the most current file.
     curr_arch=$(ls -1t ${dir_dest}/*.arc | head -1
   cat $archive_list | while read aname
   do
      if [[ $curr_arch != $aname ]]; then
        base_file=$(basename $aname)
        if [[ $OS = "Linux" ]]; then
           compress_name="${Backup_dir}/${base_file}.zip"
        else
           compress_name="${Backup_dir}/${base_file}.Z"
        fi
        cp_name="${Backup_dir}/${base_file}"
        if [[ ! -a $cp_name && ! -a $compress_name ]]; then
           # Save the file
           if [[ $OS = "Linux" ]]; then
              if [[ $COMPRESS = "Y" ]]; then
                 print "Compressing $compress_name" | $LOGCMD
                 echo "zip $compress_name $aname" >> $sav_files1
              else
                  print "Coping $aname to $cp_name" | $LOGCMD
                  echo "cp -p $aname $cp_name" >> $sav_files1
              fi
            else
              if [[ $COMPRESS = "Y" ]]; then
                     print "Compressing $compress_name" | $LOGCMD
                     echo "compress -c $aname > $compress_name" >> $sav_files1
              else
                     print "Coping $aname to $cp_name" | $LOGCMD
                     echo "cp -p $aname $cp_name" >> $sav_files1
              fi
            fi
        fi
      else
          print "Skipping $aname" | $LOGCMD
      fi
   done
   # Chmod sav_files1 and run it.
   chmod +x $sav_files1
   $sav_files1 > $sav_log1 2>&1
   if [[ $? -ne 0 ]]; then
      print "Saving files had error bailing application" |$LOGCMD
      cat $sav_log1 |$LOGCMD
      abort_pgm
   fi
 fi

}


Last edited by radoulov; 06-22-2011 at 05:07 AM.. Reason: Code tags.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Endless loop - Fork function failed?

I need a quick script that will serve as a sort of "real time monitor" for watching some log files. I am using Bourne shell in HP-UX 10.20. I have basically created a script that never ends, unless of course I manually terminate it. Here's the script (it's called qhistory): clear echo "REAL... (3 Replies)
Discussion started by: cdunavent
3 Replies

2. Shell Programming and Scripting

Endless Loop

Hi, I'm pretty new to UNIX shell scripting and need some help. We have an Informatica interface that dumps any files that have errors into a directory. I need to check that directory periodically for any of up to 9 files that might be in it and run a specific process for each file found. The... (3 Replies)
Discussion started by: JeffR
3 Replies

3. Shell Programming and Scripting

For loop in ksh..Please help..

Hi ALL, I need to take some command line arguments for my script and then want to run a function for each argument.I thought of using for loop as below, but its not working , can some one please help... #!/bin/ksh lpar1=$1 lpar2=$2 lpar3=$3 lpar4=$4 lpar5=$5 echo "$lpar1" >>lpar.txt echo... (4 Replies)
Discussion started by: prashant43
4 Replies

4. Shell Programming and Scripting

calculating endless columns

I have about 5000 columns of data that i need to convert all of it into pecentages. for shorter colums i have been using this code: {print $1/($1+$2)*100,$2/($1+$2),$3/($3+$4)*100 .....} but this is a teadious process... is there anyway to do it without having to write all of them out? sample... (20 Replies)
Discussion started by: chronicx
20 Replies

5. Shell Programming and Scripting

[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi, PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime. I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file. However, I'd like to make sure... (2 Replies)
Discussion started by: jjshell
2 Replies

6. Shell Programming and Scripting

Help with While loop in KSH

Hi, I want to write a while loop like this can any one say me whats wrong with my loop USAGE="Usage: Mail.ksh" integer i=3 while ((1<i<=3)) do . . . . (( CMD_JUL = LSD_JUL - i )) CUR_MAINT_DATE=$(julian2date ${CMD_JUL}) . . . i=i-1 done (1 Reply)
Discussion started by: bhagya2340
1 Replies

7. Shell Programming and Scripting

Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this: grep -r "searchstring" dir/subdir/ > listofoccurrences.txt For brevity sake one can enter the intended directory and use this: grep -r "searchstring" . > listofoccurrences.txt which as I found out leads to an endless loop,... (2 Replies)
Discussion started by: figaro
2 Replies

8. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

9. Shell Programming and Scripting

Script runs in endless loop

Hi, AM very new to shell scripting and try to run a simple do while loop statement, but it ends up running endlessly. please can anyone assist, dunno what am doing wrong, any useful suggestions will be welcomed. #!/bin/ksh ### To check a running process instance #################... (5 Replies)
Discussion started by: bayoo
5 Replies

10. UNIX for Beginners Questions & Answers

Help with accidental endless loop

I was practicing writing simple loops as I am a new bash user and I created this script, which turned out to be an endless loop where the echo output does not stop and I do not see where my mistake is. #!/bin/bash echo 'enter a number from 1 to 100' read number while do ... (2 Replies)
Discussion started by: goldenlinx
2 Replies
All times are GMT -4. The time now is 10:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy