0

I have a windows 2012 r2 machine with a scheduled ruby process which runs every minute - sometimes exiting after only a few seconds, other times, running for 5 minutes or so.

What I am trying to achieve is to be able to shutdown the machine, waiting for the process to exit (if running) before actually shutting down. The shutdown needs to be initiated externally (e.g. via an AWS API call for instance).

Is there a way to do this?

I have tried a few different things, but to no avail:

  • Signal.trap(...) inside the ruby code - it seems as though no signal is sent to the process
  • a shutdown script which itself calls Wait-Process - in this case, I think that the ruby process is being killed before the shutdown script executes.

I also see that there is a way for windows programs to hook in to certain windows events (e.g.: CTRL_SHUTDOWN_EVENT) and return values to say whether or not to continue shutting down, but I am a bit of a loss as to how one can hook in to this in a ruby process. Perhaps it can be wrapped in some windows program to allow this?

JonoB
  • 63
  • 7
  • I know using the win32api it's possible to create Windows Services out of Python scripts. I don't know if it's possible to do with Ruby at all - though of course it's possible that you could create a Python script to signal your Ruby script - probably via sockets, I don't think Windows supports signaling. [Looks like there is a similar Ruby library](http://win32utils.rubyforge.org/) – Wayne Werner Jan 04 '16 at 23:45

1 Answers1

1

You are using the wrong Windows mechanism for the thing you want to achive. I would rewrite the ruby process as a Windows services so Windows will signal to the Service when it's going to shutdown.

Alternatively you could schedule an extra task "On shutdown" that stops the running Ruby task and does some clean exiting as you would write in your Ruby task.

  • Although I haven't tried using a windows service as yet, having read some doc about windows services, it seems that you've put me on the correct path. Cheers. Something like http://stackoverflow.com/questions/163497/running-a-ruby-program-as-a-windows-service should be fairly easy. – JonoB Jan 07 '16 at 23:44