5

I have a Minecraft bedrock edition server running on our shared pc. I would like to interface with it via python. However, one problem I have is that my brothers sometimes restart our pc, or Windows updates. I need to know how to detect that shutdown event and send the shutdown command to the server before restart. I am using the subprocess library.

1 Answer 1

2

So, what you will need is the win32API and the function described here. You can use this function to add what's called a Control Handler Method that will run whenever the program is being shut down or terminated for any reason, including shutdown. You can find a list of the different codes that can be passed to the handler and their meanings here. Ideally, you should have a handler method that just shuts down the server, waits for it to finish shutting down, and then return.

I don't have any personal experience with the library, but it should be fairly straightforward.

EDIT: as noted by @ErykSun, you will need to create a hidden window in order to receive the events. To be quite honest I'm not sure how to create that hidden window. Some documentation suggests that running your application as a service may also work. I will look into this more if I get time.

Sign up to request clarification or add additional context in comments.

2 Comments

python.exe loads user32.dll. So even as just a console app, the system sees it as a GUI app and does not send control events for logoff and shutdown events. As suggested in the last two paragraphs in the remarks for SetConsoleCtrlHandler, the script will have to create a hidden window and handle WM_QUERYENDSESSION and WM_ENDSESSION.
@ErykSun Thanks, you are absolutely right. I will add that to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.