Eric's thoughts

ruby, rails, and more

Posts Tagged ‘linux

execute shell command in ruby rake task

with 2 comments

There are a few differnt options to execute shell cmd in ruby rake task:
1. exec
2. system
3. backtick “
4. %x{}

%x{} returns back to rake task whereas  exec and system does not (exec replace the current process with a new one, system return true or false )
so if you  do something like

desc 'example'
  task :example do
    exec {cmd_one}
    exec {cmd_two}  
  end
end

cmd two will never get executed. Switch to %x{} instead.

btw, you can run multiple rake task like this

desc 'example'
  task :example do
    Rake::Task['name_space:task_one']
    Rake::Task['name_space:task_two']
  end
end

Written by ericzou

June 20, 2009 at 5:07 pm

Posted in ruby programming

Tagged with , , ,

Follow

Get every new post delivered to your Inbox.