I have the rake tasks in my rails application. i want to run a commandline commands with in rake task. how can i do this. i tried by the following but fails
desc "Sending the newsletter to all the users"
task :sending_mail do
run "cd #{RAILS_ROOT} && ar_sendmail -o -t NewsLetters -v"
system "cd #{RAILS_ROOT} && ar_sendmail -o -t NewsLetters -v &"
end
The above run command throws run method undefined & System command not throwing any errors but not executed.
The
sh
Rake built-in is probably the best method:The interface is similar to
Kernel.system
but:echo b
This links may help you run command line command into ruby ...
http://zhangxh.net/programming/ruby/6-ways-to-run-shell-commands-in-ruby/
Calling shell commands from Ruby
http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html
run
is used by Capistrano and other things for launching commands, but Rake often makes use ofKernel#system
instead.Your command might be being run, but not working. Why not make a wrapper shell script you can test independently, or try and kick off using the full path:
It would seem odd to have your script not in
scripts/
The
system
call should returntrue
on success. If this is not the case, either the script returned an error code, or the command could't be run.