0

I have to execute simple shell commands in ruby script one after other. Mys script does the following things in order: check if ABC services running, if true then stop_services; delete directories; copy fresh directories from src to dest. Below is the code:

def stop_ABC_services
     begin
       ABC_service_cnt = "ps -fu #{user} | grep ABC  | grep -v grep | wc -l"

       while exec "#{ABC_service_cnt } >& ABC.log".to_i > 0
           exec "sh stop_ABC.sh stop >>& ABC.log"
       end

     rescue => e
          puts "Error occured in stop_siebel_services - #{e}.".red
          Kernel.exit(false)
     end
end

But it is failing at the very first method stop_ABC_services by giving the following error: Error occured in stop_ABC_services - can't convert false into String. Cannot proceed further. Quitting...

I am not able to figure out the solution for this. Appreciate the guidance to fix this.

Thanks

4
  • Well, remove your error handling or add puts e.backtrace to it and post full error message please. Commented Aug 17, 2015 at 15:37
  • 2
    You probably meant while exec("#{ABC_service_cnt } >& ABC.log").to_i > 0 Commented Aug 17, 2015 at 16:10
  • 2
    Note that exec, in accordance with the Unix system call of the same name, replaces the current process; it doesn't spawn a new one. This probably isn't what you want. Commented Aug 17, 2015 at 16:20
  • Instead of exec and the quotes, use the `<command> ` syntax. It's that button above the ~. I think exec ends the process after it executes that command. Commented Aug 17, 2015 at 23:21

3 Answers 3

1

Short answer: use parentheses (and don't use exec).

Longer answer: by not using parentheses you make ruby execute code in an order completely different from what you want. This

exec "#{ABC_service_cnt } >& ABC.log".to_i > 0

is executed in this sequence

"#{ABC_service_cnt } >& ABC.log".to_i # => 0
0 > 0 # => false
exec false # => TypeError: no implicit conversion of false into String

Should be this instead

while exec("#{ABC_service_cnt } >& ABC.log").to_i > 0

Also you might want not to use exec because it replaces the current process. Meaning, your while (and everything inside/after it) will not get a chance to run even once. Most likely you want system() call, which also handles zero/non-zero exit codes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, adding parenthesis helped in fixing the problem.
0

You could do that only executing sh

system("ps aux | grep ABC | grep -v grep  && stop_ABC.sh stop >>& ABC.log")

The meaning of that is.

&& will run stop_ABC.sh stop >>& ABC.log if ps aux | grep ABC | grep -v grep

you could find more info here

Comments

0
def stop_ABC_services
 begin
   ABC_service_cnt = "ps -fu #{user} | grep ABC  | grep -v grep | wc -l"

   while exec("#{ABC_service_cnt } >& ABC.log").to_i > 0
       exec "sh stop_ABC.sh stop >>& ABC.log"
   end

 rescue => e
      puts "Error occured in stop_ABC_services - #{e}.".red
      Kernel.exit(false)
 end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.