Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash-如何从背景ruby脚本获取标准输出_Ruby_Bash_Background Process - Fatal编程技术网

Bash-如何从背景ruby脚本获取标准输出

Bash-如何从背景ruby脚本获取标准输出,ruby,bash,background-process,Ruby,Bash,Background Process,我需要在后台运行一个ruby脚本,但我想实时查看它的输出 我编写了一个名为loop.rb的简单测试: #!/usr/bin/env ruby (1..4).each do puts "loop!" sleep 1 end 其前景输出为: sony@sonymint:~/test$ ./loop.rb loop! loop! loop! loop! 但我无法在背景中看到它: sony@sonymint:~/test$ ./loop.rb & [2] 3935 sony@son

我需要在后台运行一个ruby脚本,但我想实时查看它的输出

我编写了一个名为
loop.rb
的简单测试:

#!/usr/bin/env ruby

(1..4).each do
  puts "loop!"
  sleep 1
end
其前景输出为:

sony@sonymint:~/test$ ./loop.rb
loop!
loop!
loop!
loop!
但我无法在背景中看到它:

sony@sonymint:~/test$ ./loop.rb &
[2] 3935
sony@sonymint:~/test$ 
sony@sonymint:~/test$ ping google.com &
[2] 3734                                                                                                                                                                                                                                                                     
sony@sonymint:~/test$ PING google.com (64.233.190.113) 56(84) bytes of data.                                                                                                                                                                                                 
64 bytes from ce-in-f113.1e100.net (64.233.190.113): icmp_seq=1 ttl=42 time=79.6 ms                                                                                                                                                                                          
64 bytes from ce-in-f113.1e100.net (64.233.190.113): icmp_seq=2 ttl=42 time=79.5 ms                                                                                                                                                                                          
64 bytes from ce-in-f113.1e100.net (64.233.190.113): icmp_seq=3 ttl=42 time=81.7 ms                                                                            
但是,我可以在后台看到
ping
的输出:

sony@sonymint:~/test$ ./loop.rb &
[2] 3935
sony@sonymint:~/test$ 
sony@sonymint:~/test$ ping google.com &
[2] 3734                                                                                                                                                                                                                                                                     
sony@sonymint:~/test$ PING google.com (64.233.190.113) 56(84) bytes of data.                                                                                                                                                                                                 
64 bytes from ce-in-f113.1e100.net (64.233.190.113): icmp_seq=1 ttl=42 time=79.6 ms                                                                                                                                                                                          
64 bytes from ce-in-f113.1e100.net (64.233.190.113): icmp_seq=2 ttl=42 time=79.5 ms                                                                                                                                                                                          
64 bytes from ce-in-f113.1e100.net (64.233.190.113): icmp_seq=3 ttl=42 time=81.7 ms                                                                            
因此,有两个问题:

  • 为什么我可以看到
    ping
    的输出,而看不到
    loop.rb
    的输出

  • 如何在Bash的后台获得loop.rb的输出

  • 更新


    @TomLord的评论是对的:有一些奇怪的系统配置。在本地,我的
    ruby
    不是一个普通安装的ruby解释器;它是一个脚本,在Docker容器中运行
    ruby
    。所以这可能是一个码头工人的问题。我在一台安装了ruby解释器的机器上进行了测试,结果很好。接下来我将调查Docker缓冲问题。

    我找到了问题和解决方案

    问题是:

  • 为什么我可以看到
    ping
    的输出,而看不到
    loop.rb
    的输出
  • 因为我的本地
    ruby
    不是一个真正的ruby解释器,而是一个在docker容器中运行
    ruby
    的脚本(比使用
    rvm
    IMHO更容易管理)。这是docker管道缓冲区的问题

    解决方案:

  • 如何在Bash的后台获得loop.rb的输出
  • 通过使用:


    尝试将以下内容放在脚本的开头:

    STDOUT.sync = true
    

    这将禁用Ruby脚本标准输出上的缓冲。如果您的系统的libc检测到它没有在交互式shell中运行,则该缓冲通常由它来完成。

    对我来说很好。对我来说也很好。也许你有一些奇怪的系统配置?(但即使这样,我也不知道如果这不起作用,
    ping
    为什么会起作用?!)在Ruby测试中尝试更长的字符串;您可能存在缓冲问题,程序运行时间不够长,缓冲区无法填充并写入标准输出。@TomLord您说得对,请参阅我的更新。