Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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
在Ruby中关闭CMD_Ruby_Cmd - Fatal编程技术网

在Ruby中关闭CMD

在Ruby中关闭CMD,ruby,cmd,Ruby,Cmd,我用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) `sh

我用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中关闭窗口吗?

也许您可以使用FreeConsole功能:

例如:

require 'win32console'

puts 'Hello, world!

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

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

taskkill /f /im cmd.exe

这绝对与冬眠无关,不是吗?我想你是对的,我想我太专注于我的功能了,它与冬眠有很大关系。谢谢你的回答。问题是我不想使用Win32Console,因为它会在我非常依赖的所有字符串中产生UTF-8编码问题。有什么办法可以避免吗?谢谢。这是一个解决方案,但在我的情况下,我有点矫枉过正;