Autostart using systemd

Tags: #<Tag:0x00007f739b7d8898>

Newer Linux distributions are trending towards using systemd for managing daemons. Typically, systems based on Fedora, ArchLinux, or Debian (8 or later) use systemd. This includes Ubuntu releases including and after 15.04, CentOS, and Red Hat. If you are unsure if your system is using systemd, you may check with the following command:

ps -p 1 -o comm=

If the preceding command returns the string systemd, continue with the instructions below.


A service file is needed to control Home Assistant with systemd. The template below should be created using a text editor. Note, root permissions via sudo will likely be needed. The following should be noted to modify the template:

  • ExecStart contains the path to hass and this may vary. Check with whereis hass for the location.

  • For most systems, the file is /etc/systemd/system/[email protected]_USER.service with YOUR_USER replaced by the user account that Home Assistant will run as (normally homeassistant).

  • If unfamiliar with command-line text editors, sudo nano -w [filename] can be used with [filename] replaced with the full path to the file. Ex. sudo nano -w /etc/systemd/system/[email protected]_USER.service. After text entered, press CTRL-X then press Y to save and exit.

Python virtual environment

If you’ve setup Home Assistant in virtualenv following our manual installation guide for Raspberry Pi (or the Python installation guide), the following template should work for you. If Home Assistant install is not located at /srv/homeassistant, please modify the ExecStart= line appropriately. YOUR_USER should be replaced by the user account that Home Assistant will run as (e.g homeassistant).

The file will be called /etc/systemd/system/[email protected]_USER.service

[Unit]
Description=Home Assistant
After=network-online.target
[Service]
Type=simple
User=%i
WorkingDirectory=/home/%i/.homeassistant
ExecStart=/srv/homeassistant/bin/hass -c "/home/%i/.homeassistant"

[Install]
WantedBy=multi-user.target

Now head down to the Next Steps section

Docker

If you want to use Docker, the following template should work for you.

[Unit]
Description=Home Assistant
Requires=docker.service
After=docker.service

[Service]
Restart=always
RestartSec=3
ExecStart=/usr/bin/docker run --name=home-assistant-%i -v /home/%i/.homeassistant/:/config -v /etc/localtime:/etc/localtime:ro --net=host homeassistant/home-assistant
ExecStop=/usr/bin/docker stop -t 2 home-assistant-%i
ExecStopPost=/usr/bin/docker rm -f home-assistant-%i

[Install]
WantedBy=multi-user.target

Next Steps

You need to reload systemd to make the daemon aware of the new configuration.

sudo systemctl --system daemon-reload

To have Home Assistant start automatically at boot, enable the service.

sudo systemctl enable [email protected]_USER

To disable the automatic start, use this command.

sudo systemctl disable [email protected]_USER

To start Home Assistant now, use this command.

sudo systemctl start [email protected]_USER

You can also substitute the start above with stop to stop Home Assistant, restart to restart Home Assistant, and ‘status’ to see a brief status report as seen below.

$ sudo systemctl status [email protected]_USER
● [email protected] - Home Assistant for YOUR_USER
Loaded: loaded (/etc/systemd/system/[email protected]_USER.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2016-03-26 12:26:06 CET; 13min ago
Main PID: 30422 (hass)
CGroup: /system.slice/system-home\x2dassistant.slice/[email protected]_USER.service
├─30422 /usr/bin/python3 /usr/bin/hass
└─30426 /usr/bin/python3 /usr/bin/hass
[...]

To get Home Assistant’s logging output, simple use journalctl.

sudo journalctl -f -u [email protected]_USER

Because the log can scroll quite quickly, you can select to view only the error lines:

sudo journalctl -f -u [email protected]_USER | grep -i 'error'

When working on Home Assistant, you can easily restart the system and then watch the log output by combining the above commands using &&

sudo systemctl restart [email protected]_USER && sudo journalctl -f -u [email protected]_USER

Automatically restarting Home Assistant on failure

If you want to restart the Home Assistant service automatically after a crash, add the following lines to the [Service] section of your unit file:

Restart=on-failure
RestartSec=5s
4 Likes
Password to start HA in SSH
Installing Home Assistant Core on CentOS/RHEL
Hassio transformed in home assistant
Where is homeassistant hiding?
Forgot user and password of UI
Home Assistant Installation
Power cut kills Hassio - any fix while away from machine?
Auto start hass.io
Custom Component: systemd (Run HA as a systemd notify daemon, with watchdog support!)
Installing HA v0.41.0 in virtualenvt not create hass file for autostart using systemd
Template for [email protected][your user].service
Unable to get hassio tab on Web UI by running home assistant in python virtual environment
I need help, please
Autostart new install
Cannot get hass service to work (linux / systemd)
Help, upgrade to Stretch broke my homeassistant
Password for hass
Hass.os should be USB bootable on Raspberry Pi 3B, 3B+, 4B
Issues configuring autostart
Ubuntu Startup Script Script
How to start HA if Raspberry PI is rebooted
Everything broken in 0.50.x
Manual SSH to AsusWrt works but HASS can't connect
Basic setup
Announcement: HADashboard v2 Beta3!
Autostart for manual installation method (Virtual Environment)
Unable to autostart homeassistant on RPI3
Autostarting home assistant on rPi - launch on reboot pauses at login page
How to autostart HASS in virtualenv?
Set a delay for HASS to Start After Server Restart (systemd)
Simplest way to start after Manual Hass install on RasPi
Wink/network issues... Stumped... Help!
Updating HA in venv, I'm missing something here
Autorestart of HA on reboot
Auto-start on Raspberry pi
Help to configure Home Assistant on Rasbian!
Installing Home Assistant Core on Fedora
0.1088 problems
Failure To Launch - New Install of Hassio
HA auto start on failing
HA Stops about every 2nd day - RPi2 (SSH = ok)

Thanks good explanation got it working.

Advice to everyone new to Raspberry OS / linux.

Use “sudo nano” to sudo edit text files.
It’s harder to start but easier in the end because gksudo & other sudo methods to gui edit text files are hard to get working now in latest versions of Linux

Thank you for this! These steps should absolutely be included in the manual installation guide. Or at least include a link to this post.

Thanks for the guide. Home Assistant doesn’t start and I believe it has got to do with the folder structure. Here’s the output for sudo systemctl status [email protected]

   Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2020-09-04 19:18:58 EDT; 3min 15s ago
  Process: 5409 ExecStart=/opt/homeassistant/bin/hass -c /home/%i/.homeassistant (code=exited, status=200/CHDIR)
 Main PID: 5409 (code=exited, status=200/CHDIR)

Sep 04 19:18:58 vortexbox systemd[1]: Started Home Assistant.
Sep 04 19:18:58 vortexbox systemd[1]: [email protected]: Main process exited, code=exited, status=200/CHDIR
Sep 04 19:18:58 vortexbox systemd[1]: [email protected]: Unit entered failed state.
Sep 04 19:18:58 vortexbox systemd[1]: [email protected]: Failed with result 'exit-code'.

I have confirmed the Home Assistant install with whereis hass so I believe this is correct

what does your systemd service file look like?

As in /etc/systemd/system/[email protected]_USER.service

I have created a user homeassistant so created /etc/systemd/system/[email protected] as per the above instructions for a virtual environment (setup done via Installing Home Assistant Core on Fedora) - only change is the location of home assistant which I checked via whereis hass

Description=Home Assistant
After=network-online.target
[Service]
Type=simple
User=%i
WorkingDirectory=/home/%i/.homeassistant
ExecStart=/opt/homeassistant/bin/hass -c "/home/%i/.homeassistant"

[Install]
WantedBy=multi-user.target 

File details as follows

-rwxr-xr-x 1 root root 238 Sep  4 18:53 /etc/systemd/system/[email protected] ```