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?