4

I'm trying to execute .sh file in rails index action.

I try exec"sh app/controllers/file.sh" it's execute it in terminal then stop the server! I don't know why. And I try output = system"sh app/controllers/file.sh" it's return true in the browser not what is in the .sh file

Thanks in advance :)

IDI
  • 175
  • 1
  • 14

1 Answers1

4

You'll want to look at the Open3 class, specifically capture2 or capture3:

require 'open3'
stdout, stderr, status = Open3.capture3("sh app/controllers/myscript.sh")

As you can see above capture3 will get you the stdout, stderr, and status of your script run. While capture2 will just return stdout and status. There are other useful functions in Open3 which are worth looking at.

To understand why exec and system are behaving the way they are, you can read this SO answer: Ruby, Difference between exec, system and %x() or Backticks

Gavin Miller
  • 43,168
  • 21
  • 122
  • 188