wani
wani

Reputation: 51

How to close command prompt Window in Ruby?

I need to close the command prompt window which I have opened in my ruby script using system("start cmd.exe")

I cannot use "exit" command as there is some batch file running in the command prompt, I need to terminate that Window and close the command prompt.

Please help me to close Window.

Upvotes: 1

Views: 1668

Answers (1)

Victor Deryagin
Victor Deryagin

Reputation: 12225

I'd do something like:

cmdprompt = Process.spawn 'start cmd.exe' # open prompt
Process.kill 9, cmdprompt                 # close it

Not sure will it work for Windows, though.

There is gem win32-process that provides ability to handle external processes on Windows, try this:

require 'rubygems'
require 'win32/process'
cmdprompt = Process.create(:app_name => 'cmd')   # open
Process.kill 9, cmdprompt.process_id             # close

I have no ability to test it, but think it shoul work.

Upvotes: 2

Related Questions