From: Michael Guterl Date: 2010-02-22T07:41:27+09:00 Subject: Re: Difference between system() versus exec Kernel methods On Sun, Feb 21, 2010 at 5:23 PM, Alex DeCaria wrote: > I'm trying to understand the difference between the Kernel.system and > Kernel.exec methods.  It was explained in an earlier post that .system > started a new process while keeping the old one running, while .exec > replaced the current process with the new one (if I read it correctly). > > I created a simple program called test.rb as follows > > put "hello" > gets > puts "bye" > > and then called it from another program that was simply > > system("ruby test.rb") > > or > > exec "ruby test.rb" > > I then looked at the processes in Task Manager while running the second > program which calls the first.  I expected that with system("ruby > test.rb") I would see two ruby processes being run, which indeed > occured. > > When I ran the second version with exec "ruby test.rb" there were still > two active Ruby processes being run (I expected only one, since one > process would be replaced by the other). > > So, what am I missing?  What is the difference between .system and > .exec? > ri Kernel#exec ------------------------------------------------------------ Kernel#exec exec(command [, arg, ...]) ------------------------------------------------------------------------ Replaces the current process by running the given external _command_. If +exec+ is given a single argument, that argument is taken as a line that is subject to shell expansion before being executed. If multiple arguments are given, the second and subsequent arguments are passed as parameters to _command_ with no shell expansion. If the first argument is a two-element array, the first element is the command to be executed, and the second argument is used as the +argv[0]+ value, which may show up in process listings. In MSDOS environments, the command is executed in a subshell; otherwise, one of the +exec(2)+ system calls is used, so the running command may inherit some of the environment of the original program (including open file descriptors). exec "echo *" # echoes list of files in current directory # never get here exec "echo", "*" # echoes an asterisk # never get here ri Kernel#system ---------------------------------------------------------- Kernel#system system(cmd [, arg, ...]) => true or false ------------------------------------------------------------------------ Executes _cmd_ in a subshell, returning +true+ if the command was found and ran successfully, +false+ otherwise. An error status is available in +$?+. The arguments are processed in the same way as for +Kernel::exec+. system("echo *") system("echo", "*") _produces:_ config.h main.rb * I can't explain it any better than the docs. Best regards, Michael Guterl