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

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

:confused:

1 Like

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.

 a=$((a+1))
 

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

Thanks,
R. Singh

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.

#!/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.

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.

 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

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:

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

Dear Singh/RudiC,
Thanks it worked, reduced this to

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

as against ;

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