How to execute commands within Rake tasks?

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.


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

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