Ask A Question


You’re not receiving notifications from this thread.

Spawn process and send data to STDIN when data avilable

Drazen asked in Ruby

I want to spawn a process (golang binary), capture its STDOUT and have a reference to STDIN which i can send to at any time. In my case i am getting data in via Websockets and want to pass them to STDIN of the spawned process.

I am having issues reusing STDIN with various methods i tried, like using PTY.spawn

input = nil

def run
  cmd = "app/bins/transcode" 
  read, write, pid = PTY.spawn(cmd)
  input = write
  Signal.trap(:WINCH) { write.winsize = STDOUT.winsize }

  read.each do |line|
    # THis works fine and sends the spawned process' STDOUT over websockets
    ActionCable.server.broadcast 'shell_channel', test: line

# This is never sent to STDIN of the spawned process
input.puts "hello"

I tried additionally to use a lock syntax with PTY and popen3 but both failed to receive STDIN.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,149+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.