【问题标题】:Display output of the command over SSH on run time在运行时通过 SSH 显示命令的输出
【发布时间】:2017-02-01 02:38:45
【问题描述】:

我正在通过 ssh 运行 perl 脚本,如下所示:

[dipuh@local ~]$ ssh dipuh@myremote_001 'perl /home/dipuh/a.pl'

a.pl的内容如下:

print "Sleeping \n";
sleep(60);
print "Waking Up";

在这里,我的本地终端等待 perl 脚本完全执行,完成后会显示完整的输出。最初的“睡眠”文本也将仅与最终输出一起打印。

有什么办法,在我的本地终端中,我可以在运行时显示 perl 脚本中每个命令的输出,而不是等待整个 perl 脚本完成。

【问题讨论】:

    标签: perl shell unix ssh


    【解决方案1】:

    你是suffering from buffering

    您可以将 $| 设置为 1 用于块。

    {
        local $| = 1;
        print "Sleeping \n";
        sleep(60);
        print "Waking Up";
    }
    

    或者使用 IO::Handle

    use IO::Handle;
    STDOUT->autoflush(1);
    

    【讨论】:

      【解决方案2】:

      您可以尝试打开自动冲洗模式。老式的方法是在脚本顶部添加以下内容:

      $| = 1;
      

      或者你可以用更现代的方式来做:

      use IO::Handle;
      
      STDOUT->autoflush(1);
      

      或者,您可以使用以下命令按需刷新 STDOUT:

      use IO::Handle;
      
      print "Sleeping \n";
      STDOUT->flush;
      sleep(60);
      print "Waking Up";
      STDOUT->flush;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多