Is it possible to reload and restart a standalone ruby script (windows and mac)?
I have a master worker architecture where the number of workers grows weekly. I can no longer expect ssh or the remote console on each machine to kill the worker, sync to the source, and restart. I would like to be able to post a message on the net that tells each machine to sync and reboot.
The fact that I got to the checkpoint. If I were using any sane platform, I could simply do:
exec('ruby', __FILE__)
... and let's do it. However, I did the following test:
p Process.pid
sleep 1
exec('ruby', __FILE__)
... and on Windows I get one ruby instance for each exec call. None of them die until I put ^ C on the appropriate window. On every platform I tried this, every time it launches a new version of the file and I tested this by doing simple changes in the test script while the test passed.
The reason I am printing the pid is to double check the behavior I see. On Windows, I get a different pid with each execution - which is what I expect given that I see a new process in the Task Manager for each run. The mac is behaving correctly: the pid is the same for every system call, and I checked with dtrace that every run causes a call to sysall exec.
So, in short, is there a way to get a ruby Windows script to restart its execution so that it runs any code - including itself - that changed during its execution? Please note that this is not a rails app, although it does use activerecord.
a source to share
After trying a number of solutions (including the one presented by Byron Whitlock, which ultimately set me on the path to a satisfying end), I decided:
IO.popen("start cmd /C ruby.exe #{$0} #{ARGV.join(' ')}")
sleep 5
I have found that if I have not slept at all after surfacing and have just exited, it spawns frequently (> 50% of the time). Obviously it is not cross platform, so in order to have the same behavior on mac:
IO.popen("xterm -e \"ruby blah blah blah\"&")
a source to share