Ruby on Rails
Tuesday, October 13, 2015
On Tuesday, 13 October 2015 07:56:28 UTC-4, Sam S wrote:
I am trying to run a ruby script from my rails controller.The script runs successfully when the rails server is started normally asrails s thin -e developmentBut when rails server is started as a daemon, the ruby script fails to run.rails s thin -e development -dInside the controller the ruby script is run with the **exec** command.script_exec = exec("ruby /Code/AttendanceReport/attn/bin/scripts/Spreadsheet_ proper.rb '#{city}' '#{date1}' '#{date2}'") The above approach kills the rails server and doesn't execute the ruby script. And hence I usedscript_exec = %x{ruby /Code/AttendanceReport/attn/bin/scripts/Spreadsheet_ proper.rb '#{city}' '#{date1}' '#{date2}' } script_exec = system("ruby /Code/AttendanceReport/attn/bin/scripts/Spreadsheet_ proper.rb '#{city}' '#{date1}' '#{date2}'") The above **%x** and **system** approaches also fail when run rails server is started with -d.
How do they "fail"? Is there any output in the terminal, in the system log, or the Rails server logs? If you wrap the call in a begin/rescue block, what is the value in $? Example:
exec_result = begin
system("do a thing")
rescue => e
$stderr.puts(e.inspect)
$stderr.puts($?)
exit(-1)
end
Also, I'd recommend being VERY CAREFUL with string interpolation around strings that are handed off to the shell (as the one-argument version of `system` does). If someone enters their city as "; rm -rf / ; echo" you're going to have a very bad time, depending on your system's permissions. Prefer the multi-argument version of `system` whenever possible:
system("command name", "arg1", "arg2", "arg3")
In this form, the arguments are passed straight through to the called program, without ever being expanded / manipulated by the shell.
--Matt Jones
-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/74646212-663b-4679-b4fe-88f3f5d7616d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment