[Solved] Restarting Home Assistant from the command line (without restarting container) + other cmds (works with Docker)

Hi,
I was searching for a solution to this for a while as I didn’t like restarting my docker container every time I made a config change.

For Docker, it turns out that everything comes back up much quicker if we just restart HA inside the container rather than restarting the whole container.

We can use the Restart button in the Configuration->General page but I also wanted to be able to trigger that from the command line so that it can be quickly restarted after saving any changes to yamls etc (which I do in through the SSH terminal). I’ve also incorporated this into a script which quickly checks that the config is okay, and if it is then it restarts HA.

After some digging on the API pages I realised you can call a service with curl:

curl -X POST -H "x-ha-access: yourpass" -H "Content-Type: application/json" <IP_address>:8123/api/services/homeassistant/restart

Note the -H "x-ha-access: yourpass" may not be required depending on your authorisation configuration.

Also, some other useful commands:

  • Reload Automations:
    curl -X POST -H "x-ha-access: yourpass" -H "Content-Type: application/json" <IP_address>:8123/api/services/automation/reload
  • Reload Groups:
    curl -X POST -H "x-ha-access: yourpass" -H "Content-Type: application/json" <IP_address>:8123/api/services/group/reload
  • If using Docker - Check Config (can easily be amended for non-docker):
    docker exec -ti home-assistant python -m homeassistant --config /config --script check_config

You can combine some of these in a script to automatically update elements if files are changed as detailed by kristoffernolgren in his post here

4 Likes

huh???

hassio ha restart 

works!

5 Likes

Good point.

I’ve never used HASSIO but was just trying to make the point that these commands are generic (except the last one) so should work will all variations.

Will edit my title to remove that bit

For docker, something like docker restart homeassistant should work, assuming the container is tagged as homeassistant.

3 Likes

That’s what I used to do. But as I said in my post, I wanted to just restart HA without restarting the whole container. It turns out everything comes back up much quicker if we just restart HA inside the container without restarting the whole container.

1 Like

When I ran HA in Docker I noticed the computer took a LOT longer to shut down, almost like some process was hanging.

I don’t see any difference between restarting the container and the docker restart homeassistant or restarting it from the GUI.

I didn’t realise it was slow until I was watching some YouTube videos where people were restarting HA and it was coming back up really quickly. And they weren’t using Docker. So I thought there must be a better way to do it rather than bringing the whole container down. It was taking 4-5 min each time to get everything fully restored.

I figure each time the container is restarted it needs to find/renegoitiate all the connections to other devices/services (from looking at the logs Ring & Nest were taking the most time) but there must be some caching or something which allows the connections to be persistent if you don’t bring down the container.

2 Likes

I wonder if it depends on what is configured in HA?

From looking at the logs, when I bring down the container it seems to take a while connecting with my Nest thermostat and Ring doorbell (also Ikea stuff but to a lesser extent). But if I restart without bringing down the container then it doesn’t hang when connecting with them at all.

But this usually takes longer than restarting from the API/GUI (at least on my experience). Are you sure they are equivalent?

1 Like

Yes I’m sure.

1 Like

Today I tried to use ui-lovelace.yaml, but forget to paste initial UI config. So blank screen - no ability to restart through the web.

For me killall -HUP hass worked fine for reload.

In a Debian Supervisor installation use just type ha at a root ssh session to open the HA CLI:

root@HAssistant:~# ha

The Home Assistant CLI is a small and simple command line utility that allows
you to control and configure different aspects of Home Assistant

Usage:
ha [command]

Available Commands:
addons Install, update, remove and configure Home Assistant add-ons
audio Audio device handling.
authentication Authentication for Home Assistant users.
banner Prints the CLI Home Assistant banner along with some useful information
cli Get information, update or configure the Home Assistant cli backend
core Provides control of the Home Assistant Core
dns Get information, update or configure the Home Assistant DNS server
docker Docker backend specific for info and OCI configuration
hardware Provides hardware information about your system
help Help about any command
host Control the host/system that Home Assistant is running on
info Provides a general Home Assistant information overview
jobs Get information and manage running jobs
multicast Get information, update or configure the Home Assistant Multicast
network Network specific for updating, info and configuration imports
observer Get information, update or configure the Home Assistant observer
os Operating System specific for updating, info and configuration imports
resolution Resolution center of Supervisor, show issues and suggest solutions
snapshots Create, restore and remove snapshot backups
supervisor Monitor, control and configure the Home Assistant Supervisor

Flags:
–api-token string Home Assistant Supervisor API token
–config string Optional config file (default is $HOME/.homeassistant.yaml)
–endpoint string Endpoint for Home Assistant Supervisor (default is ‘supervisor’)
-h, --help help for ha
–log-level string Log level (defaults to Warn)
–no-progress Disable the progress spinner
–raw-json Output raw JSON from the API

Use “ha [command] --help” for more information about a command.

2 Likes

Just in case someone searches like I did, the above command’s didn’t work for me in the command line via VM. but, “core restart” did

9 Likes

Host restart

This will reboot the whole system

10 Likes

This one worked for me!

core restart
or
host restart

What’s the difference?

Core restart just restarts home assistant
Host restart will reboot the host eg debian or HA OS

2 Likes

Thank you David.
Every now and then, HA stops functioning. Well, the UI doesn’t do anything, HA is unavailable from chrome browser, Always, so far, in the middle of the night.
I’m running HA on a NAS in a virtual machine.
The console is available, so being able to do a core restart is very handy in that case.
And it worked perfectly.

2 Likes

Tried out the curl at the top of the page, and it didn’t work for me with 401, so just wanted to post the solution. I’m assuming the auth mechanism has just been updated since the post.

curl -X POST -H "Authorization: Bearer $(HA-APIKey)" \
     -H "Content-Type: application/json" \
     $(HA-URL)/api/services/homeassistant/restart

Also - if you’re getting 403s (likely from getting too many 401s) then take a look at the auto-ban yaml file (i just deleted it!)

1 Like