Logic to combine 3 steps in one loop

Hi

i am trying to find a logic where, i will be able to combine 3 steps in
loop.

currently the way it working is

#Function to find out , if there is any java process running ornot
def java_process()

    process_id=`ps -ef | grep -v grep | grep java | awk '{print

$2}’`
return process_id

end

#Try twise by using /etc/init.d/storage_stop. (STEP 1)
count = 0
while count < 2

    puts "Trying Count: #{count} ,Stoptting services"
    #stop_all_services()
    process_array = java_process()
    if !process_array.empty?
            puts "Still java proess running"
            puts process_array
    else
            break;
    end

count +=1
end

if Java Process still does not stop then kill it manually by 15 .

(STEP 2)

process_array = java_process()
if !process_array.empty?
puts "Java Process Running "
puts “Killing by 15”

else
puts “All Process is Dead”
end

#If its still does not stop kill by 9 (STEP3)

process_array = java_process()
if !process_array.empty?
puts "Java Process Running "
puts “Killing by 9”

else
puts “All Process is Dead”
end

What i want is :

the whole steps will run on a loop , from start off.

As long as there is java process will run, its will try to kill 2 times
by STEP1, then will go STEP2 then sTEP3

but From step 1 to step 3 , if it see there is not any java process, it
will break the main loop

Can any one please help me with the logic

Thanks