This is a weird one. I have a shutdown script that I thought worked, but it turned out I fooled myself. I start a shell script from a script, with the “restart.sh” shellscript as the service. This works:
I would guess that HA starts a process to run the script. But the script stops HA, so any process started by HA (to run in the foreground) is probably also stopped.
I can’t say I’m an expert at this stuff, but wouldn’t it make sense to see if shutting down the system while HA is running stops HA as part of the natural shutdown process?
Or as another alternative, could you try adding & to the end of the HA shell_command? Or if that doesn’t work, have the shell command run one shell script that runs a second shell script using &, and then the second shell script runs the two commands. (P.S. Sorry for all the edits!)
Thanks, I will have a look at that tomorrow (it’s past 01 here). I have tried a few different versions, with exec that fires another script from the script in different ways, and with & and && but no luck so far. I read here on the forum that the most stability concious guys wants to stop Hass before the reboot because a regular reboot is too brutal. It does stop Hass but it can stop it mid writing to the database, as far as I could understand.
I tried the different versions, but got nowhere. Then I found a solution. An ugly solution, but it’s a solution: I used sudo visudo to add the second shell script to the things that everybody could run sudo without a password:
ALL ALL=(ALL) NOPASSWD: /home/homeassistant/.homeassistant/boot.sh
(Note that it’s a tab between the first two ALL, or Visudo will refuse to save it because it breaks your configuration.)
Then I used the first script to call the second script sudo from the shell command:
And finally I made the actual reboot script into one line:
#!/bin/bash
sleep 10 && /usr/bin/sudo /sbin/shutdown -r now
Here I give Hass time to exit gracefully, and it works every time. 15 seconds does not! Why I have no idea. But Hass stops at around 7-8 seconds, so it’s not a problem.