-
use backticks when you want to easily capture the output of a program in a variable. you probably only want to use this for short-running programs, because this will block.
-
systemis convenient in two different cases:a. You have a long running program and you want the output to print as it runs (e.g.
system("tar zxvf some_big_tarball.tar.gz"))b.
systemcan bypass the shell expansion likeexec(compare the output ofsystem "echo *"andsystem "echo", "*")system blocks until the subprocess has exited.
-
forkhas a couple different use cases as well:a. You want to run some ruby code in a separate process (e.g.
fork { .... }b. You want to run a child process (or different program) without blocking progress of your script
fork { exec "bash" }.forkis your friend if you want to daemonize your program. -
IO.popenis useful when you need to interact with the standard out and standard in of a program. Note that it doesn’t capture standard err, so you need to redirect that with2>&1if you care about that. -
popen3gives you a separate file descriptor for standard error (for when you need to capture that separately from standard out) -
PTY.spawnis necessary when you want the spawned program to behave like you are running from the terminal. See the difference ofgrep --color=auto pat filewhen spawned withsystemvsPTY.spawn