Script that stops HASS, updates HASS and starts HASS again?

Hi,

I’m getting a bit tired of running the commands to update my HASS setup everytime.
I have it running in a VirtualEnv, so i need to:

  1. Switch to the VirtualEnv
  2. Stop HASS
  3. Run the --install command
  4. Start HASS again

My goal is to have a script that i can start and that does all the work for me. That way i only have to type one command, instead of 5

Is there someone who has a nice (Bash?) script for this?

3 Likes

I’ve looked at this a couple of times - the trouble is that the virtual_env needs to activated for the running shell, not the child shells that spawn to run the commands. I suspect that it will need to be a bash function to work correctly - still a WIP for me :slight_smile:

1 Like

I’m looking to achieve the same thing (I’m also using a VirtualEnv, via the All-In-One Raspberry Pi installer) – if anyone comes up with a solution please post!

1 Like

Ran into the same problem and gave up for nobler pursuits. :grin: Glad to see I wasn’t the only one… or the only one with an interest for this.

I automated the update process using the following script:

#!/bin/bash

# Become user 'hass'
sudo su -s /bin/bash hass <<'EOF'
# Activate the virtualenv
source /opt/home-assistant/bin/activate
# Install Home Assistant
pip3 install --upgrade homeassistant
exit
EOF

# Restart Home Assistant
sudo systemctl restart home-assistant@hass

The trick is to combine all commands that need to be run by the hass user into a section that starts and ends with EOF.

This script works on Ubuntu 16.04.1 LTS in a virtual environment - please adapt the path to your installation passed into the source command.
As you can see, I am actually updating the software without stopping it, and instead restart after the update has finished. The update just takes seconds here, and I haven’t seen issues. Might be safer to stop first, then update, then start.

11 Likes

Works great - thanks!

Sweet! Thanks very much!!

That seems to work indeed.

I added the stop command to be a little more safe and changed the paths to reflect my situation:

#!/bin/bash

# Stop HASS
sudo systemctl stop home-assistant@hass
# Become user 'hass'
sudo su -s /bin/bash hass <<'EOF'
# Activate the virtualenv
source /srv/hass/bin/activate
# Install Home Assistant
pip3 install --upgrade homeassistant
exit
EOF

# Restart Home Assistant
sudo systemctl restart home-assistant@hass
9 Likes

works great, thanks for that :slight_smile:

Perfect! Thank you :slight_smile:

Im trying to run this script from HA (Raspberry Pi venv) but Im getting this:

[sudo] password for homeassistant:

The script runs fine out of the virtual environment. I tried changing the script location to /home/homeassistant/.homeassistant/scripts/hass_update.sh without success.

My script:

#!/bin/bash

# Become user 'hass'
sudo su -s /bin/bash homeassistant <<'EOF'
# Activate the virtualenv
source /srv/homeassistant/bin/activate
# Install Home Assistant
pip3 install --upgrade homeassistant
exit
EOF

# Restart Home Assistant
sudo systemctl restart home-assistant@homeassistant

I have this on the shell_commands file:

#Update Home Assistant
hass_update: "/home/pi/hass_update.sh"

and this on the scripts folder:

hass_update.yaml

sequence:
- service: shell_command.hass_update

Any help? Thanks!

You’d have to add the HA user to sudoers as all command line scripts executed from within HA are executed as the HA user.

2 Likes

I think that at some point I added the user to the sudoers.

pi@raspberrypi:~ $ sudo su -s /bin/bash homeassistant
homeassistant@raspberrypi:/home/pi $ source /srv/homeassistant/bin/activate
(homeassistant) homeassistant@raspberrypi:/home/pi $ groups
homeassistant sudo video gpio i2c spi

Do I need to add something else?

Run:

sudo nano /etc/sudoers

Do you see a line similar to this?

hass ALL=(ALL) NOPASSWD: ALL

The user wasn’t there, i added the user and now is working. The only thing is that is not working is the reboot at the end of the scrip. I have to manually reboot the Pi.

Thank you!

1 Like

The last line of your script is actually not attempting to reboot the Pi, but is just supposed to restart the application:

# Restart Home Assistant
sudo systemctl restart home-assistant@homeassistant

Is the restart failing? When you allowed your homeassistant user to sudo execute ALL commands, this should include the systemctl command.

Hi all, can some help me with the same issue?

I’m running HA on a Pi3 and this is my script:

#!/bin/bash

sudo systemctl stop home-assistant
sudo su -s /bin/bash homeassistant
source /srv/homeassistant/homeassistant_venv/bin/activate 
pip3 install --upgrade homeassistant 
sudo systemctl start home-assistant

Everytime I try to run the script it get stucked on the first step.

Please notice that the user homeassistant have been added to sudoers.

Thank you in advance

What method did you use to install HA?
A basic missing piece in your script is the lack of the EOF bracketing your commands.Try

sudo systemctl stop home-assistant@homeassistant

at the command line to see if that shuts down HA. If that works, change the paths to match your install.

Copied from further up in the post.

#!/bin/bash
sudo systemctl stop home-assistant@homeassistant
# Become user 'hass'
sudo su -s /bin/bash homeassistant <<'EOF'
# Activate the virtualenv
source /srv/homeassistant/bin/activate
# Install Home Assistant
pip3 install --upgrade homeassistant
exit
EOF

# Restart Home Assistant
sudo systemctl restart home-assistant@homeassistant

I used the istruction in this page: Getting started - Home Assistant

I also use the istruction described below:

To upgrade the All-In-One setup manually:

Login to Raspberry Pi ssh pi@your_raspberry_pi_ip
Change to homeassistant user sudo su -s /bin/bash homeassistant
Change to virtual enviroment source /srv/homeassistant/homeassistant_venv/bin/activate
Update Home Assistant pip3 install --upgrade homeassistant
Type exit to logout the hass user and return to the pi user.

If i try to use the command

sudo systemctl stop home-assistant@homeassistant
I receive this error:

Failed to stop [email protected]: Unit [email protected] not loaded.

This is my script, which also upgrades the OS first - seems to be working OK…

Link removed

runs from within HA with shell command:

upgrade_hass: /bin/bash /home/hass/.homeassistant/extras/bash_scripts/upgrade-hass.sh

(obviously my hass user is in the sudoers list)