Ruby run external program stops script

318 views Asked by At

I have a ruby script that midway through I need it to run another program.

After running the program the rest of the script doesnt get run. For example:

# some ruby that gets run

exe = "Something.exe"
system(exe)

# some ruby that doesnt run

I have also tried using Open3.popen2e(cmd) and Open3.popen3(cmd) but its the same.

Can anyone help me understand what is happening here and how to fix it?

note: I'm using windows

3

There are 3 answers

0
thaleshcv On BEST ANSWER

Try to run Something.exe in a new Thread:

Thread.new { system("Something.exe") }

1
mabe02 On

In case you want to run your System.exe asynchronously and continue without waiting it to be finished, you could use spawn or multithreading.

pid = spawn('System.exe')
Process.detach(pid)

According to this previous answer, this should work on Windows as well (while fork or other methods don't).

In this article you can find several examples using system, exec, fork, spawn and Thread on Unix.

I cannot reproduce it, but it could be worth to see if using system("start System.exe") works on windows like system("cmd &") works on UNIX. You can refer to start documentation here.

0
Mike Slinn On

I used spawn from a Ruby program running on WSL to launch djv (a native Windows program). The Ruby program exits soon after, and djv continues. Here is the code:

realpath = File.realpath copy_filename
windows_path = `wslpath -m '#{realpath}'`.chomp
spawn 'cmd.exe', '/c',
      "C:\\Program Files\\DJV2\\bin\\djv.com",
      windows_path