Search code examples
ruby-on-railsshellcommand-linerakerake-task

How to execute commands within Rake tasks?


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.


Solution

  • 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

    %x[command].each do |f|
      value = f
    end