LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 10-13-2011, 06:38 AM   #1
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Rep: Reputation: Disabled
Unhappy Need while loop which runs after it finds file.


Hi,

I have one cron which runs at 5AM. the script is below.

cron
====
00 5 * * 2-6 /usr/home/progress/bin/stage_5b.sh

script
======
SCRIPTDIR=/emc/reale/apps/pseudo/mss/src/
DLC=/usr/dlc
export DLC
PF=$SCRIPTDIR/stage5.pf

$DLC/bin/mbpro -pf $PF -p $SCRIPTDIR/stage5/ncre-stg5.p

I want to make this run only if it finds prolog.log file in path /stage5_dp04/dump. below loop is not ending. Can anybody help.


Modified script
================
SCRIPTDIR=/emc/reale/apps/pseudo/mss/src/
DLC=/usr/dlc
export DLC
PF=$SCRIPTDIR/stage5.pf

cd /stage5_dp04/dump
while [ -f /stage5_dp04/dump/prolog.log ]
do
$DLC/bin/mbpro -pf $PF -p $SCRIPTDIR/stage5/ncre-stg5.p
exit
done
 
Old 10-13-2011, 06:54 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you put the exit statement inside the while loop, it exits after the first iteration, hence you don't really need a loop: an if/then construct should be enough. If you don't see the loop terminates after a while, maybe its due to a long execution time of the mbpro command (or it sticks for some reason).
 
Old 10-13-2011, 07:19 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Adding to what colucix says, you don't need a loop:
Code:
if [ -f /stage5_dp04/dump/prolog.log ]
then
     $DLC/bin/mbpro -pf $PF -p $SCRIPTDIR/stage5/ncre-stg5.p
fi
You could throw in an else to simply say, "The file does not exist" or something but that's a little overkill.

Hope this helps some.
 
Old 10-13-2011, 08:26 AM   #4
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Will it try again if it doesnt find the prolog.log file in path. Coz the timing is 5AM, the file might get generated till 8AM (depends on some other process). So this loop has to try again and again and when found the file prolog.log it has to run this.?
 
Old 10-13-2011, 09:12 AM   #5
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
That's not a good idea; what if your file doesn't show up at all; you will have another script start up at 5am the next day, and more after that. You will then need to ensure that only one script starts up at a time.
 
Old 10-13-2011, 09:19 AM   #6
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
It will show up.. because this script runs mon to sat and prolog.log will showup anytime after 3am but have to make sure after prolog.log gets created our 5am script has to run. Here some time instead of 3am prolog.log file gets genereated at 6am. So if i check this condition and run my 5am script then it will run without a hitch. About another script running at that time doesnt matter here. Please advise. Can the above "if" condition full fills my need.
 
Old 10-13-2011, 09:30 AM   #7
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
ok, if you're sure it will show up...

no, the "if" is not sufficient; you can change your while loop like so:

Code:
while [ ! -f /stage5_dp04/dump/prolog.log ]
do
   # wait for file to show up
   sleep 10
done

# out of loop ==> we have the file
$DLC/bin/mbpro -pf $PF -p $SCRIPTDIR/stage5/ncre-stg5.p
 
Old 10-13-2011, 09:40 AM   #8
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
OK, as you say, your /stage5_dp04/dump/prolog.log file may not be there at 0500.

So, do a loop
Code:
while true
do
     # is the file there?
     if [ -f /stage5_dp04/dump/prolog.log ]
     then
          # yup, it is, do our thing
          $DLC/bin/mbpro -pf $PF -p $SCRIPTDIR/stage5/ncre-stg5.p
          # all done, get outta here
          break
     fi
     # nope, ain't there; sleep for a half hour
     sleep 30m
done
# now that we've done our thing, let's get rid of that file
rm /stage5_dp04/dump/prolog.log
Note that your version of sleep may not like the m (minutes) suffix; in that case just multiple whatever number of minutes you want to delay by 60 and use that value -- check your manual page for sleep.

Hope this helps some.
 
Old 10-13-2011, 09:41 AM   #9
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thank a lot. i will check and let you know.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
VIM runs into an indefinite loop while editing a large file xuancong Linux - Software 1 04-12-2011 08:56 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
Why For loop in C runs infinitely when only 'equal to' operator is given in condition rmnature Programming 7 02-24-2009 08:28 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 08:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration