【问题标题】:Suppressing Command Prompt output while running a shell command in capture3在 capture3 中运行 shell 命令时抑制命令提示符输出
【发布时间】:2021-06-06 18:02:12
【问题描述】:

我正在通过 capture3 在 Ruby 脚本中运行 Amazon's Kindle Previewer 工具。 Kindle Previewer 命令验证 epub 文件并将日志打印到指定文件夹,同时还在终端运行时打印日志。我确实希望这些消息中的任何一个出现在终端中。命令本身的语法是kindlepreviewer [epub file] -log -output [log output folder]。在我的 capture3 语句中,它看起来像这样:

stdout, stderr, status = Open3.capture3("kindlepreviewer #{epub_file} -log -output #{output_folder}")

这可以在 Mac 和 PC 上成功执行,并且我可以在脚本的其他地方使用输出。在 Mac 上,终端窗口会抑制命令运行时生成的输出,这正是我想要的。我的问题是,在 Windows 中,所有输出仍然来自命令提示符。我不知道如何关闭它。基于类似的问题,例如:Suppressing the output of a command run using 'system' method while running it in a ruby script,我尝试了类似:

stdout, stderr, status = Open3.capture3("#{cmd} #{epub} -log -output #{@kindle_folder} > /dev/null 2>&1")

这根本没有效果。正常执行,所有输出仍然出现在命令提示符中。

我知道 Kindle Previewer 是一个非常具体的工具,可以在这里引用,但我似乎找不到更通用的答案来解释为什么在 Mac 上运行 capture3 命令会抑制终端中的输出,但在终端中运行它Windows 命令提示符不会。有什么不同的方法可以让我在 PC 上使用 Ruby 运行 shell 命令,同时还能将命令的输出存储在脚本中?

【问题讨论】:

    标签: ruby windows command-prompt popen3


    【解决方案1】:

    我自己也遇到过类似的问题,我想完全阻止 Windows 上的任何控制台输出。请记住,Windows DOS 提示符不是 Bash shell,因此您确实不能使用相同的输出重定向语法。

    在尝试了几个不同的选项后,我最终使用了win32/process,它可以直接访问 Win32 API CreateProcess 方法。只有使用这种方法,我才能完全防止输出:

    require 'win32/process'
    
    pinfo = Process.create({ command_line:   "program arguments",
                             creation_flags: Process::DETACHED_PROCESS })
    
    # Taken from the win32/process manual, in case you need to wait for the
    # spawned process to finish before continuing with the rest of your script:
    sleep 0.1 while !Process.get_exitcode(pinfo.process_id)
    

    DETACHED_PROCESS 这是关键,因为它可以防止创建的进程对原始终端的任何访问。

    使用gem install win32-process 安装这个gem。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2010-11-30
      • 2016-11-26
      • 2013-08-01
      相关资源
      最近更新 更多