Reputation: 621
I try to turn my pc off and restart it on LAN.
When getting one of the commands (turnoff or restart), I execute one of the followings:
subprocess.call(["shutdown", "-f", "-s", "-y"]) # Turn off
subprocess.call(["shutdown", "-f", "-r", "-t", "-c", "-y"]) # Restart
I'd like to inform the other side if the process was successfully initiated, and if the PC is in the desired state.
I know that it is possible to implement a function which will check if the PC is alive (which is a pretty good idea) several seconds after executing the commands, but how one can know how many seconds are needed? And what if the PC will be shut down a moment after sending a message stating that it is still alive?
I'm curious to know- what really happens after those commands are executed? Will the script keep running until the task manager will kill it? Will it stop running right after the command?
Upvotes: 0
Views: 53
Reputation: 40013
Programs like shutdown
merely send a message to init
(or whatever modern replacement) and exit immediately; it’s up to it what happens next. Typical Unix behavior is to first shut down things like SSH servers (which probably doesn’t kill your connection to the machine), then send SIGTERM to all processes, wait a few seconds (5 is typical) for signal handlers to run, and then send SIGKILL to any survivors. Finally, filesystems are unmounted and the hardware halt or reboot happens.
While there’s no guarantee that the first phase takes long enough for you to report successful shutdown
execution, it generally will; if it’s a concern, you can catch the SIGTERM to buy yourself those few extra seconds to get the message out.
Upvotes: 1