background preloader

Processes spawning & IPC

Facebook Twitter

Issue 2.1: Ways to load code - Practicing Ruby. Practicing Ruby Issue 2.1 :: Published by Gregory Brown on August 23, 2011 There are many ways to load Ruby code, and that has lead to confusion over the years. In this article, I will give you the backstory behind several conventions seen in the wild and share some stories about how I use those conventions in my own code. The topic of code loading breaks up naturally into two subtopics: loading code within your own project and loading code from third-party libraries. People tend to struggle more with loading code properly within their own projects than they do with loading code from third-party libraries, so that's what I'll focus on exclusively in this issue. For now, I will focus on the basic mechanics of load(), auto_load(), require(), and require_relative().

I'll discuss how they work so you can then think about how they can be used within your own projects. Kernel#load Suppose we have a file called calendar.rb that contains the code shown here: >> load ". >> load ". >> load ". >> load ". A dozen (or so) ways to start sub-processes in Ruby: Part 1 | The Devver Blog. Introduction It is often useful in Ruby to start a sub-process to run a particular chunk of Ruby code.

Perhaps you are trying to run two processes in parallel, and Ruby’s green threading doesn’t provide sufficient concurrency. Perhaps you are automating a set of scripts. Or perhaps you are trying to isolate some untrusted code while still getting information back from it. Whatever the reason, Ruby provides a wealth of facilities for interacting with sub-processes, some better known than others. In the first and second articles, I’ll demonstrate some of the facilities for starting sub-processes that Ruby possesses out-of-the-box, no requires needed. Getting Started To begin, let’s define a few helper methods and constants which we’ll refer back to throughout the series. (Note: The full source code for this article can be found at Next, let’s define a couple of helpful constants. Method #1: The Backtick Operator 1. Method #2: Kernel#system This results in: 2. 3. Tech.natemurray.com: 6 Ways to Run Shell Commands in Ruby.

Often times we want to interact with the operating system or run shell commands from within Ruby. Ruby provides a number of ways for us to perform this task. Kernel#exec (or simply exec) replaces the current process by running the given command For example: $ irb >> exec 'echo "hello $HOSTNAME"' hello nate.local $ Notice how exec replaces the irb process is with the echo command which then exits. The system command operates similarly but the system command runs in a subshell instead of replacing the current process. system gives us a little more information than exec in that it returns true if the command ran successfully and false otherwise. $ irb >> system 'echo "hello $HOSTNAME"' hello nate.local => true >> system 'false' => false >> puts $?

System sets the global variable $? System is great if all we want to know is “Was my command successful or not?” Backticks (also called “backquotes”) runs the command in a subshell and returns the standard output from that command. Notice that $? 0? Ruby Programming/Running Multiple Processes. There are several ways to run external commands in Ruby. output = `command here` # gives you back full stdout stdout_and_stdin = IO.popen("command here") require 'popen3' # require 'open3' in 1.9 input,output,error,running_thread_on_19_or_greater = Open3.popen3("command here") or the same: Open3.popen3("command here") do |stdin, stdout, stderr| # ... end pid = fork { puts 'in child process' } # posix platforms only pid = Process.spawn "ls" # 1.9.x only pid = Process.daemon "ls" # 1.9.x only, basically does a spawn and a disassociate on that pid.

Process.spawn (1.9 only) has many options. Accessing PID's of running processes[edit] For many of these commands there is no way to get the pid of a running child process while it is still running. For fork you get it immediately via $? If you want the pid and I/O of a running sub-process, you'll have to use fork and redirect the child processes I/O to previously created pipe's, or jruby users have an available In reality all that $?

While ! Ex: Ruby Programming/Running Multiple Processes. Executing commands in ruby | BigBinary Blog. Ruby allows many different ways to execute a command or a sub-process. In this article we are going to see some of them. backtick backtick returns the standard output of the operation. output = `ls` puts "output is #{output}" Result of above code is $ ruby main.rboutput is lab.rbmain.rb Note that backtick operation forks the master process and the operation is executed in a new process. If there is an exception in the sub-process then that exception is given to the main process and the main process might terminate if exception is not handled. In the following case I am executing xxxxx which is not a valid executable name. outut = `xxxxxxx`puts "output is #{output}" Result of above code is given below.

. $ ruby main.rb main.rb:1:in ``': No such file or directory - xxxxxxx (Errno::ENOENT) from main.rb:1:in `<main>' To check the status of the backtick operation you can execute $?. Output = `ls`puts "output is #{output}"puts $?. $ ruby main.rb output is lab.rb main.rb true system exec sh popen3 popen2e.

Tech.natemurray.com: 6 Ways to Run Shell Commands in Ruby. Ruby Kernel system, exec and %x. The Ruby Core Library documentation is very similar for Kernel.system, Kernel.exec and %x[..]. Recently I needed to kick off a system process, so I spent some time working with all 3 options. Kernel.exec does exactly what the documentation states: 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. An important thing to notice is that it replaces the current process. Focus:~/work/eaa jay$ irbirb(main):001:0> exec 'svn st'focus:~/work/eaa jay$ Kernel.system behaves very similarly, but does not replace the current process.

Security - Forming sanitary shell commands or system calls in Ruby. Module: Open3. Open stdin, stdout, and stderr streams and start external executable. In addition, a thread for waiting the started process is noticed. The thread has a pid method and thread variable :pid which is the pid of the started process. Block form: Open3.popen3([env,] cmd... [, opts]) {|stdin, stdout, stderr, wait_thr| pid = wait_thr.pid ... exit_status = wait_thr.value } Non-block form: stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts]) pid = wait_thr[:pid] # pid of the started process. ... stdin.close # stdin, stdout and stderr should be closed explicitly in this form. stdout.close stderr.close exit_status = wait_thr.value # Process::Status object returned.

The parameters cmd... is passed to Process.spawn. Open3.popen3("echo abc") {|i, o, e, t| ... } Open3.popen3("echo", "abc") {|i, o, e, t| ... } Open3.popen3(["echo", "argv0"], "abc") {|i, o, e, t| ... } If the last parameter, opts, is a Hash, it is recognized as an option for Process.spawn. Getting output of system() calls in ruby. Module: PTY. Check(pid, raise = false) => Process::Status or nil click to toggle source check(pid, true) => nil or raises PTY::ChildExited Checks the status of the child process specified by pid.

Returns nil if the process is still alive. If the process is not alive, will return a Process::Status or raise a PTY::ChildExited (if raise was true). pid The process id of the process to check raise If true and the process identified by pid is no longer alive a PTY::ChildExited is raised. Returns nil or a Process::Status when raise is false. static VALUE pty_check(int argc, VALUE *argv, VALUE self) { VALUE pid, exc; pid_t cpid; int status; rb_scan_args(argc, argv, "11", &pid, &exc); cpid = rb_waitpid(NUM2PIDT(pid), &status, WNOHANG|WUNTRACED); if (cpid == -1 || cpid == 0) return Qnil; if (!

Getpty(command_line) { |r, w, pid| ... } click to toggle source getpty(command_line) => [r, w, pid] getpty(command, args, ...) { |r, w, pid| ... } getpty(command, args, ...) => [r, w, pid] command_line The full command line to run r w. Process Management and Communication. Shell - Continuously read from STDOUT of external process in Ruby. Working With TCP Sockets - A short, concise guide that teaches Rubyists the basics of socket programming, and then some! Do you know how your web server opens a socket, binds to an address, and accepts a connection? I did a lot of web programming before I had enough knowledge to dig in and figure this stuff out. I knew that other developers had a better grasp on the full stack than I did, but diving deep under the hood is one of the things that really made me a better developer all around.

I recently read a great thread that asked "What did the really successful programmers do differently? ". This response really caught my eye: Be ready, willing, & able to deep dive multiple levels at any time. You must know what's going on under the hood. There is a strong correlation between "number of levels of deepness understood" and "programming prowess".

In this book I'll teach you these fundamentals using Ruby. Learning this stuff doesn't just apply to Ruby, or any other language. Working With Unix Processes — Learn the Fundamentals of Unix Programming in Ruby. You'll receive an email with a download link immediately after purchase. No login required. Download includes PDF, ePub, MOBI, TXT, and source code. Books about Unix programming can be quite an investment.

They're typically 1000+ pages, and cost anywhere from $60 - $100. When you buy Working With Unix Processes you get the book, case studies, plus the web server project and any future updates for only $39 USD. My name is Jesse Storimer. For the past 3 years I've been solving hard problems at Shopify, one of the largest, busiest Ruby on Rails sites on the web. My journey into the world of Unix programming started while working on Shopify's infrastructure and continues today. And, yes, I'm working on my Unix beard. If it's not working for you then it's not working for me. If, for any reason, you're not happy with this book just send me an email: I'll give you your money back and you keep everything.

Practicing_spawning.rb. Issue 5.5: Process spawning patterns - Practicing Ruby. This article was contributed by Jesse Storimer. He is the author of Working with Unix Processes and Working with TCP Sockets, a pair of ebooks providing fundamental Unix knowledge to Ruby developers. When he's not at the keyboard, he's often enjoying the great Canadian outdoors with his family. Like many of you, I discovered Ruby via Rails and web development. That was my "in. " task :console do `irb -r my_app`end There's something simple and beautiful in the combination of Ruby and the command line here--the backticks are barely detectable. Ruby provides many ways of spawing processes. Task :console do system('irb -r my_app')end Or what about exec? Task :console do exec('irb', '-r', 'my_app')end In order to make this decision, you need to understand what these methods are doing under the hood. In this article, we're going to reimplement the key parts of these process-spawning primitives to get a better understanding of how they work and where they're most applicable.

Starting somewhere.