Script runs in endless loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script runs in endless loop
# 1  
Old 07-22-2015
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.

Code:
#!/bin/ksh    

### To check a running process instance

################# readgfile process #################

a=`ps -ef | grep readgfile | grep -v grep | grep -v "readgfile /AFF /SNV1" | grep "/AFF /SNV"|wc -l `
while [ $a -lt 4 ]
do 
						echo "... readgfile /AFF /SNV is not running, trying to start it"
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_$sach.log 2>> $LOG/readgfileNV_$sach.err &          
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_2_$sach.log 2>> $LOG/readgfileNV_2_$sach.err &      
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_3_$sach.log 2>> $LOG/readgfileNV_3_$sach.err &      
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_4_$sach.log 2>> $LOG/readgfileNV_4_$sach.err & 
done


##########  END #########

Smilie
This User Gave Thanks to bayoo For This Post:
# 2  
Old 07-22-2015
Wrench

Hello bayoo,

Welcome to forum, a special thanks to you for using code tags for commands and codes used in your post. I can see there is NO increment given in your code for variable named a so condition in loop will be always TRUE that's why it is going to be endless, please add so and this should be fine then, though I am not sure about your complete requirement but for your question this should to the trick.

Code:
 a=$((a+1))

Add this before the end of the loop, as per thumb rule.

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-22-2015 at 08:26 AM..
# 3  
Old 07-22-2015
Dear Singh,

Thanks for the quick response. Let me try give more insight to what am trying to do. This is like a check script to make sure that the number of instance for a running process doesnt not fall short than expected.

For example,

The process readgfile is suppose to run 4 instance at a time but for one reason one or two of the instance goes down, i want to be able to check the count of instance running and automatically restart thhose failed instance from this script.


I have added like this, but still noticed it runs more that expected instance. Any suggestion please.


Code:
#!/bin/ksh    

### To check a running process instance

################# readgfile process #################

a=`ps -ef | grep readgfile | grep -v grep | grep -v "readgfile /AFF /SNV1" | grep "/AFF /SNV"|wc -l `
while [ $a -lt 4 ]
do 
						echo "... readgfile /AFF /SNV is not running, trying to start it"
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_$sach.log 2>> $LOG/readgfileNV_$sach.err &          
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_2_$sach.log 2>> $LOG/readgfileNV_2_$sach.err &      
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_3_$sach.log 2>> $LOG/readgfileNV_3_$sach.err &      
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_4_$sach.log 2>> $LOG/readgfileNV_4_$sach.err & 

a=$((a+1))

done


##########  END #########


BR


Bayoo.
# 4  
Old 07-22-2015
Hello bayou,

I am seeing you have mentioned you need to check count of process readgfile but you have given grep -v "readgfile /AFF /SNV1" which means grep will negate the search where string "readgfile /AFF /SNV1" is coming in any process. So you can try with following.
Code:
 a=`ps -ef | grep readgfile | grep -v grep | grep "readgfile /AFF /SNV1" c -l `

Also if above doesn't work, then following are my suggestions on same.

1st: Try to search for exact process which you want to monitor.
2nd: Are these four process which you mentioned are related to each other means, is there one parent id which is going to create rest of child ids? If yes then you should have only script which is supposed to STOP and START readgfile process. If you have separate all 4 readgfile processes then you may need to start the exact process which is DOWN in spite of starting all process.
3rd: Add some more points if needed please to be better understanding on this.

Hope this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-22-2015 at 08:51 AM..
# 5  
Old 07-22-2015
You need to reevaluate the process count within the loop; and you should launch only as many new peocesses as are needed. Try sth along this line:
Code:
a=$(ps | grep -c "[r]eadgfile /AFF /SNV ")
while [ $a -lt 4 ]
  do $BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_$a_$sach.log 2>> $LOG/readgfileNV_$a_$sach.err & 
     a=$(ps | grep -c "[r]eadgfile /AFF /SNV ")
  done

# 6  
Old 07-22-2015
Dear Singh/RudiC,
Thanks it worked, reduced this to

Code:
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_$sach.log 2>> $LOG/readgfileNV_$sach.err &

as against ;

Code:
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_$sach.log 2>> $LOG/readgfileNV_$sach.err &     
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_2_$sach.log 2>> $LOG/readgfileNV_2_$sach.err & 
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_3_$sach.log 2>> $LOG/readgfileNV_3_$sach.err & 
$BIN/readgfile /AFF /SNV /D0 1>> $LOG/readgfileNV_4_$sach.log 2>> $LOG/readgfileNV_4_$sach.err &

Thanks guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Script only runs as a particular user

Hi guys So I've got this PERL script that for one reason or another I need to run as a user other than the user that created the script. When I su - to another user the script won't run and doesn't give me any output as to why. No permission denied or anything like that. I've chmod 777'd the... (5 Replies)
Discussion started by: Jaymoney
5 Replies

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

4. Shell Programming and Scripting

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... (18 Replies)
Discussion started by: Sylvan303
18 Replies

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

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

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

8. Shell Programming and Scripting

Shell Script: want to insert values in database when update script runs

Hi , I am new to linux and also also to shell scripting. I have one shell script which unpacks .tgz file and install software on machine. When this script runs I want to insert id,filename,description(which will be in readme file),log(which will be in log file) and name of unpacked folder... (1 Reply)
Discussion started by: ring
1 Replies

9. 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
Login or Register to Ask a Question