20

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
palani
  • 4,661
  • 8
  • 31
  • 36

3 Answers3

29

Rake sh built-in task

This is probably the best method:

task(:sh) do
  sh('echo', 'a')
  sh('false')
  sh('echo', 'b')
end

The interface is similar to Kernel.system but:

  • it aborts if the return is != 0, so the above never reaches echo b
  • the command itself is echoed before the output
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
7

run is used by Capistrano and other things for launching commands, but Rake often makes use of Kernel#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:

newsletter_script = File.expand_path('ar_sendmail', RAILS_ROOT)

if (File.exist?(newsletter_script))
  unless (system(newsletter_script + ' -o -t NewsLetters -v &'))
    STDERR.puts("Script #{newsletter_script} returned error condition")
  end
else
  STDERR.puts("Could not find newsletter sending script #{newsletter_script}")
end

It would seem odd to have your script not in scripts/

The system call should return true on success. If this is not the case, either the script returned an error code, or the command could't be run.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Hi tadman, Thanks for your answer... i just copied your code and tried but not working as expected..can you please tell me what is File.expand_path do ? – palani Sep 08 '10 at 14:38
  • Hello tadman, your script looking for the file Rails_Root/ar_sendmailer. but this is not a file and this is command which invoked from Rails app root folder. like [/home/username/railsapp]$ar_sendmail -o -t NewLetter -V . so i rake file i go the Rails application root folder and trigger the above command that what i need... please help me on this – palani Sep 08 '10 at 15:00
  • `File.expand_path` is used to...expand paths, basically. It will properly join two paths together, like appending `ar_sendmail` on your Rails path, whatever that is. It's also good for eliminating things like `../../` when required, giving you the proper, full path. – tadman Sep 08 '10 at 16:54
  • You need to know where `ar_sendmail` is otherwise you won't be able to launch it properly. Your shell has a `PATH` which can differ from what Ruby will use, and that can make scripts work even when they're not in the directory you're in. – tadman Sep 08 '10 at 16:59
6

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
Community
  • 1
  • 1
krunal shah
  • 16,089
  • 25
  • 97
  • 143
  • Rake has a specific method for running a shell command, `#sh`. It has the shortcoming of using `/bin/sh`, not bash however.To use bash from Rake's `sh` method, see this answer to a duplicate question: https://stackoverflow.com/questions/9796028/execute-bash-commands-from-a-rakefile/34523379#34523379 – Binary Phile Dec 30 '15 at 05:35