AlexGustafsson / homebridge-wol

A Wake on Lan plugin for Homebridge
MIT License
301 stars 30 forks source link

After first successful command, no response to state changes #127

Closed noktulo closed 4 years ago

noktulo commented 4 years ago

I can successfully use homebridge-wol once, and then it will not respond until I restart homebridge.

Hitting the switch successfully turns off the computer, but the switch in the Home app has a spinner for a bit, then shows the device as On but with an exclamation mark. If I hit the button, I can still change the status of the device in Home, but nothing happens.

Here's my relevant debug log:

[11/1/2020, 11:30:44 AM] [Mike's PC] [NetworkDevice] Got user-triggered state change: 0 [11/1/2020, 11:30:44 AM] [Mike's PC] [NetworkDevice] Shutdown cycle started [11/1/2020, 11:30:44 AM] [Mike's PC] [NetworkDevice] Device went from status "Online" to "ShuttingDown" [11/1/2020, 11:30:44 AM] [Mike's PC] [Pinger] Stopping pinger [11/1/2020, 11:30:44 AM] [Mike's PC] [NetworkDevice] Attempting to shut down the device using "ssh" [11/1/2020, 11:31:23 AM] [Mike's PC] [NetworkDevice] Got user-triggered state change: 0 [11/1/2020, 11:31:25 AM] [Mike's PC] [NetworkDevice] Got user-triggered state change: 1 [11/1/2020, 11:32:26 AM] [Mike's PC] [NetworkDevice] Got user-triggered state change: 0

You can see it shut down the computer, and you can see the triggers from me tapping the button in Home on and off afterwards, with no action taken from Homebridge.

This happens both on the stable and v5 beta release.

AlexGustafsson commented 4 years ago

It looks like the SSH command opens up an interactive prompt or shell, meaning the command will never finish. I think there’s a timeout option you could use, but better yet is to make sure that the SSH channel is terminated as soon as you’ve run your command. You can for example try ssh user@host bash -c 'shutdown && exit'. This is also quite common if the SSH server prompts for your credentials, so make sure that the user Homebridge is running as has SSH key authentication setup so that it may access the server without a password (and therefore without a prompt).

I think this could be made clearer by the plugin if “executing a command” would translate to a state. That way the plugin would deadlock and make it able to throw errors whenever you try to trigger a state change again whilst the command is still running. Another way to mitigate this is to simply implement a default timeout which will then log a warning if the time runs out. Definitely worth looking into for the next release.

noktulo commented 4 years ago

Ah, good call! That makes a lot of sense, and thanks for the quick response. I'm sshing into Windows, and just adding "&& exit" to the end of the command exits without running the first command, so I'll need to do more research.

noktulo commented 4 years ago

This is the actual command I'm using for reference: "ssh -f -i /homebridge/pc_id_rsa michael@192.168.1.15 '%windir%/System32/rundll32.exe powrprof.dll,SetSuspendState 0,1,0'"

Actually I lied, it seems that running "ssh -f -i /homebridge/pc_id_rsa michael@192.168.1.15 '%windir%/System32/rundll32.exe powrprof.dll,SetSuspendState 0,1,0 & exit'" or && exit both put the computer to sleep but don't actually exit.

noktulo commented 4 years ago

This seems to have fixed it:

ssh -f -i /homebridge/pc_id_rsa michael@192.168.1.15 -o ServerAliveInterval=1 '%windir%/System32/rundll32.exe powrprof.dll,SetSuspendState 0,1,0'

As soon as the PC stops responding, the SSH connection is killed.

AlexGustafsson commented 4 years ago

Great thing you were able to solve it and thanks for posting the solution here, it's always helpful as a future reference.