1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

The ‘exec’ Method


There are several ways to run other programs in Ruby. There is the system method, backticks, etc. However, they all spawn new processes. If there is nothing left for your Ruby program to do, it has to sit there wasting memory until the launched program finishes. The exec method fixes this.

The Process::exec method is available on most systems. It may not be available on some Windows systems, so your results may vary. It will certainly be available on Linux and OSX systems, however.

What Process::exec does is replace the process image with that of another program. Behind the scenes, the system and backticks will fork the current Ruby process (that is, make an identical copy of it in memory) then call the exec system call, which replaces that copy with a new program. This is the basis of how many shells and operating systems work. The exec method is something you often see behind the scenes, but you can use it yourself if you like.

So what does it mean to “replace the process image with that of another program” mean? Examine the following code.

#!/usr/bin/env ruby

puts `date` # Print the date and time
puts “Check 1”

exec ‘date’ # Do the same
puts “Check 2”

The date command on many systems will print the date and time. The first time the date command is run, Ruby will fork off a copy of itself and use exec to run the date command. The first copy of Ruby (which is busy running your program) will stop and wait for the new copy to complete. The copy will gather the ouput of the command and return it back to the original copy of Ruby, which it passes to the puts command. Ruby will then continue running your program, going on to the Check 1 line.

But here’s where using exec directly differs. The second time the date command is run, Ruby won’t fork off a copy of itself. It simply tells the Operating System to replace itself with the date command and run it. There is no second process, and nothing but the shell is waiting for it to return. Your Ruby program will never be returned to, because it no longer exists in memory and isn’t being run. The second Check 2 line will never be run, there is no Ruby interpreter left to run it.

So what use is the exec method? At first, it seems silly to use it yourself, that it’s something best left behind the scenes. And that’s true, there aren’t many times when you’d use something like this. One case that really stands out is the command wrapper. A command wrapper is something often run from the command line that exists only to run another program. Maybe that program has complicated arguments that need to be figured out (by your Ruby command wrapper program), or a host of other reasons. Running Ruby programs is not free, and while modern computers are fast and have quite a lot of memory, launching many such command wrappers at once will add up quickly. So, if your Ruby programs ever need to run another program and it’s the last thing the Ruby program will do, remember the exec method.

  1. About.com
  2. Technology
  3. Ruby
  4. Advanced Ruby
  5. The ‘exec’ Method

©2014 About.com. All rights reserved.