【问题标题】:Close CMD within Ruby在 Ruby 中关闭 CMD
【发布时间】:2014-08-11 07:05:03
【问题描述】:

我在 Ruby 中编写了一个函数,该函数根据参数休眠、重新启动或关闭系统。下面是它的外观:

def sysaction(action)
  sleep 0.2
  if action == "h"
    countdown(60)
    `shutdown.exe /h`
  elsif action == "r"
    countdown(60)
    `shutdown.exe -r -f t 00`
  elsif action == "s"
    countdown(60)
    `shutdown.exe -s -f t 00`
  end
end

我想添加一段代码来退出父 CMD 窗口。如果不通过批处理文件运行 ruby​​ 脚本,我找不到任何解决方案。我尝试了以下命令:

`exit`
exec `exit`
system('exit')
`cmd /C exit`

但似乎两者都不起作用。有什么方法可以从 Ruby 中关闭窗口?

【问题讨论】:

  • 这和hibernate完全没有关系,不是吗?
  • 我想你是对的,我想我太专注于我的功能,它必须与休眠做很多事情。

标签: ruby cmd


【解决方案1】:

也许你可以使用FreeConsole函数:

例如:

require 'win32console'

puts 'Hello, world!

# free console so it can be shut after the program terminates.
Win32::Console::API.FreeConsole

【讨论】:

  • 感谢您的回复。问题是我不想使用 Win32Console,因为它会在我严重依赖的所有字符串中产生 UTF-8 编码问题。有没有办法绕过这个?
【解决方案2】:

一种方法是杀死所有 cmd.exe 应用程序:

taskkill /f /im cmd.exe

【讨论】:

  • 谢谢。这是一个解决方案,但在我的情况下我有点矫枉过正;)
猜你喜欢
  • 2010-12-04
  • 2012-10-02
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多