Reboot Pi with script

@mefistofelis Rather than do something that breaks security on the pi, try something different. You can execute commands remotely using a script. On Mac or Linux you can use a key based ssh login to remotely run a script. On Windows, I think you can do the same with plink (part of the Putty package). A double click on shortcut on your desktop could do the reboot.

1 Like

I was struggling to get this done also and managed to get it done this way

Hope it helps someone

4 Likes

This works well for me, thanks a lot!

Iā€™ve been trying to setup something like this for a while (but failed).

Iā€™m getting the following error when trying to execute this:

2018-09-26 20:49:51 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `sudo reboot`, return code: 1
NoneType: None

What platform are you running on? HassIO (resinOS), HassIO (HassOS), Virtual Enviroment, Hassbian?

This option likely wonā€™t work for the first two options.

Iā€™m running Hassbian on RP3

The problem here is that you are trying to sudo from homeassistant not as the pi user since HA runs under the homeassistant account. Only pi is automatically granted sudo access.

One solution is to allow home assistant permission to run /sbin/shutdown by adding the following to /etc/sudoers.d/020_homeassistant_hassbian-scripts
You can also just add a file. For example you can make a new file named 025_homeassistant_custom-scripts and place it in the same /etc/sudoers.d folder

%homeassistant ALL= NOPASSWD: /sbin/shutdown *

now you can create the shutdown command as

shell_command:
  reboot_pi: '/usr/bin/sudo /sbin/shutdown -r now'
  shutdown_pi: '/usr/bin/sudo /sbin/shutdown now'

I also add the ability to script a HA restart by adding the following

%homeassistant ALL= NOPASSWD: /bin/systemctl restart [email protected]
shell_command:
  reboot_pi: '/usr/bin/sudo /sbin/shutdown -r now'
  shutdown_pi: '/usr/bin/sudo /sbin/shutdown now'
  ha_restart: '/usr/bin/sudo /bin/systemctl restart [email protected]'
5 Likes

I donā€™t quite understand, can you eleborate?

It would help if you explained what part you didnā€™t understand. It really depends on your level of unix administration experience. Guessing that is low let me try to elaborateā€¦

Hassbian has two default users.
pi - standard pi user in all instances of raspbian (has sudo access to become root)
homeassistant - special user used to run HA (does NOT have sudo access to become root, this is GOOD)

when you ssh into hassbian you do so as pi user.
Then you run hashell to become homeassistant. hashell is an alias it just runs the command to sudo to the new user ā€œalias hashell=ā€˜sudo su -s /bin/bash homeassistantā€™ā€

When HA runs it runs as ā€œhomeassistantā€ user. This means HA canā€™t do anything that requires a root user. That is by design or bad things could be injected into your server so you donā€™t want to change homeassistant to have full root access.

But you want HA to issue the command ā€œsudo shutdown -r nowā€. But sudo is a request to become root, homeassistant user is not allowed to do that.

The information I provided in my last post allows you to grant permission to homeassistant user to run ONLY shutdown, as root.

So by adding the permission to sudoers.d for homeassistant to run shutdown as root the command should now work.

I hope that explains why the commands I provided would solve the issue. If not please state the parts you are not understanding.

I figured this all out because I have System Admin experience and I googled the heck out of why this wasnā€™t working when I first attempted. :slight_smile:

2 Likes

@smart thanks for your very detailed explenation. Loud and clear!

I did not understand that 020_homeassistant_hassbian_scripts was actually a file so I was in the assumption you made a typo there.
I managed to add the lines now using sudo nano /etc/sudoers.d/020_homeassistant_hassbian-scripts and all is working fine now.

Thanks a lot for your help (Y).

2 Likes

BTW: The reasoning for my suggestion to create a new file is that you will not have to worry about an update overwriting the 020_homeassistant_hassbian_scripts file. By creating your own file you can be sure it will remain.

2 Likes

This is just weird! I have tried several different versions, with the one you have, and every time I add a new file and add anything to it, like the simplest command:

ALL ALL=(root) NOPASSWD: /sbin/reboot

Then I mess up all sudo access to the Pi. It says that thereā€™s a syntax error in the file, and no valid sudoers source found.

Edit: I managed to fix it by using Visudo. And I think I know what I did wrong, I believe there is a tab there that I didnā€™t get through the text here.

@Mastiff not sure what is wrong on your pi. Did you try the steps I provided above?

I use

%homeassistant ALL= NOPASSWD: /sbin/shutdown *, /bin/systemctl restart [email protected]

and it works fine

Well, I got that part working. :slight_smile: But triggering it is another dealā€¦ I have in my script:

   reboot_pi:
     alias: Omstart av Pi
     sequence:
       - alias: omstart av Pi
         service: shell_command.pi

   reboot_ha:
     alias: Restart Home Assistant
     sequence:
        service: homeassistant.restart

And then in the file shell_command.yaml in the same directory:

pi: "sudo reboot"

But that does nothing. The Hass restart works, though.

Very cool! Thanks - will try out tomorrow. Adding systemctl to ha no password would be a security issue?

My config only grants it to user homeassistant.
This lets me issue a reboot from my HA ui.
I do this by calling a shell script but I added a input.text where I enter a password and pass it back to the script. Then in the script I confirm the password before I issue the command. This way only I can issue the command from the UI. I also have my UI behind my firewall so only someone on my local network could get to it anyway. Seems pretty secure to me.

I have a panel with the following to make this all work. You enter the action password and then select the action. If it works the Action-result is OK if not it tell you what went wrong. (for example password incorrect)
image

But how is your actual command? Is it in a separate file, like my shell_command.yaml? And what is it? I am hoping I can get this to trigger this way:

rfxTRX on Node-Red on the same Pi (I have to use that - long story) sends a reading to MQTT every time a temperature sensor comes in. If that reading doesnā€™t come for 30 seconds it means that either the rfxTRX has locked up or Node-RED. That means that I have to reboot the Pi to get it working again. So first of all I would like the actuall reboot to work, and then Iā€™ll go on to the trigger setup.

Two things are probably tripping you up.

  1. Make sure the script is running as a correct shell script. Example for bash make sure you #!/bin/bash.
  2. Make sure you use full paths.

For example this is my action script.

#!/bin/bash

export actionToTake=$1

case "${actionToTake}" in
  "reboot now")
    /usr/bin/sudo /sbin/shutdown -r now
    ;;
  "shutdown now")
    /usr/bin/sudo /sbin/shutdown now
    ;;
  "ha restart")
    /usr/bin/sudo /bin/systemctl restart [email protected]
    ;;
esac

Thanks! I have spent hours googling, trying and messing around, and I have gotten further, but Iā€™m still quite stuck. Probably some idiot error. I have this scripts.yaml:

reboot_pi:
  alias: Omstart av Pi
  sequence:
    service: shell_command.omstart
reboot_ha:
  alias: Restart Home Assistant
  sequence:
    service: homeassistant.restart

The bottom part is no problem, that works. Theoretically the first one (omstart means reboot in Norwegian) should call the shell script omstart.sh, which looks like this:

#!/bin/bash

echo "$(date "+%m%d%Y %T") : Starter omstart"

echo "$(date "+%m%d%Y %T") : Stopper HASS"
sudo systemctl stop home-assistant &
echo "$(date "+%m%d%Y %T") : HASS stoppet"

echo "$(date "+%m%d%Y %T") : Omstart"
sudo reboot &
echo "$(date "+%m%d%Y %T") : Omstart gjort"

echo "$(date "+%m%d%Y %T") : omstart.sh ferdig"

I have made the script runable with sudo chmod +x omstart.sh and I have tested it as the homeassistant user with sudo -u homeassistant -H -s and that works perfectly. I have also added this to the configuration.yaml:

# Shell Commands #
shell_command: !include shell_command.yaml

And in that file (shell_command.yaml) I have:

omstart:
  alias: omstart
  sequence:
    - service: shell_command.omstart

But there is something wrong, because when I run the sript from the switch on the UI of Hass I get this:

Unable to find service shell_command/omstart

Can you see the error of my ways there?

I think actually I have gotten a step further. By changing shell_command.yaml to this:

omstart:
  alias: omstart
  sequence:
    - omstart:shell_command.omstart

I get another error:

Error running command: OrderedDict([('alias', 'omstart'), ('sequence', ['omstart:shell_command.omstart'])]), return code: 2

So now it has found the service omstart, but itā€™s not running it correctly.