Eric's thoughts

ruby, rails, and more

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
About these ads

Written by ericzou

June 20, 2009 at 5:07 pm

Posted in ruby programming

Tagged with , , ,

2 Responses

Subscribe to comments with RSS.

  1. Thanks Eric! That’s exactly what I was looking for.

    michal kuklis

    July 27, 2009 at 9:19 pm

  2. I know this is an old post, but since I found this via Google it might help someone else.

    Another way to execute a shell task in rake is:

    sh %{command}

    This has the advantage of showing the command that is executed if you do “rake –trace”. Unfortunately, this doesn’t display when you are doing a dry run — one thing I miss from make.

    Tim Binder

    January 12, 2012 at 10:00 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: