Why isn't this shutdown/restart setup working?

After hours of tinkering I have fially found a working reboot setup, but I would like to shutdown Hass gracefully first. I have tried with a script is like this:

omstart_pi:
  alias: Omstart av Pi
  sequence:
    service: shell_command.omstart
    service: homeassistant.stop

The shell_command.yaml looks like this:

omstart: /home/homeassistant/.homeassistant/omstart.sh 2>&1 | tee -a /home/homeassistant/.homeassistant/omstart.log

The extra part after the omstart.sh writes all output from the actual shell script to a log file. This is the omstart.sh:

#!/bin/bash
echo "$(date "+%m%d%Y %T") : HASS stoppet"
sleep 10
/usr/bin/sudo /sbin/shutdown -r now &
echo "$(date "+%m%d%Y %T") : Omstart igangsatt"
echo "$(date "+%m%d%Y %T") : omstart.sh ferdig"

The 10 seconds sleep is there to give Hass time to exit before the reboot. But when I click the script on the GUI I get an error message saying “failed to start service script/turn_on”. Then Hass is stopped, but , the reboot is not done. And that works perfectly when I dont use the extra service to stop home assistant after the shell_command.omstart. Can anybody please tell me why?

Oh, and am I just working needlessly here? Is it unnecessary to stop Hass gracefully before a reboot?

HomeAssistant user doesn’t have sudo privileges by default.

I was probably unclear. With this

Then Hass is stopped, but , the reboot is not done. And that works perfectly when I dont use the extra service to stop home assistant after the shell_command.omstart.

I mean that the reboot works perfectly without the extra service to stop Hass. So the reboot is no problem, that’s as easy as adding homeassistant ALL=(ALL) NOPASSWD: /sbin/shutdown to the sudoers file with sudo visudo.

I suspect I am barking up the right tree, finally. Process tree, to be precise. :wink: I have added this to sudoers:

ALL ALL=(ALL) NOPASSWD: /bin/systemctl stop home-assistant.service

Then I am trying to run this command as sudo in the virtual environment (after having gone into that with sudo -u homeassistant -H -s But this still isn’t working, when I try to run the command with sudo /bin/systemctl stop home-assistant.service@homeassistant It does not complain about a missing password for sudo, but it throws up:

Failed to stop home-assistant.service: Unit home-assistant.service@homeassistant not loaded.

I have tried without both the final @homeassistant and .service@homeassistant, but that does not help either. I am guessing I’m pretty close, but missing the final piece of the puzzle.

It depends on how your systemd service file, but the hassbian definition of stopping the service is

sudo systemctl stop [email protected]

Thank you! :slight_smile: The placement of the .service and the @homeasisstant was the problem! :slight_smile: It is necessary to use the full path, though, or sudo visudo errors out. So if anybody stumbles over this, the correct line is this:

ALL ALL=(ALL) NOPASSWD: /bin/systemctl stop [email protected]

Be aware that there is a tab between the first two ALLs, and not the spaces that it seems to be here. That messed it up a few times. The sudoers file is hysterically sensitive to any sythanx errors. And for those who wants to play with this, you need to back up your full setup first (using the built-in SDCard Copier utility), or you will never manage to recover from an error in the sudoers file.

1 Like