安装 - 自启

优质
小牛编辑
124浏览
2023-12-01

Home Assistant 针对树莓派的发行版(手动安装除外)中都已经自带自启任务,其余系统安装的用户需要自行配置自启。

systemd

目前,新进的 Linux 发行版都倾向使用 systemd 作为进程管理器。使用本方法实现 Home Assistant 的自动启动,请先确认系统使用 systemdps -p 1 -o comm=


生产环境安装

如果你未使用 Python 虚拟环境安装 Home Assistant,请使用以下方法。

sudo nano -w /etc/systemd/system/home-assistant@用户名.service

复制粘贴

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=%i
ExecStart=/usr/bin/hass

[Install]
WantedBy=multi-user.target

Ctrl + XYEnter 保存退出


虚拟环境安装

如果你的 Home Assistant 运行在 Python 虚拟环境中,请使用以下方法:

sudo nano -w /etc/systemd/system/home-assistant@用户名.service

复制粘贴

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=%i
ExecStart=/srv/homeassistant/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

Ctrl + XYEnter 保存退出

注意,如果你的 Home Assistant 没有安装在 /srv/homeassistant 路径下,请修改 ExecStart=之后的路径。


Docker

sudo nano -w etc/systemd/system/home-assistant@用户名.service

复制粘贴

[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

启动自启服务

完成上述步骤后,请重新加载进程管理。

sudo systemctl --system daemon-reload

启用服务:

sudo systemctl enable home-assistant@用户名

macOS

只需要输入以下指令

hass --script macos install

Home Assistant 就会实现自启,同时,运行日志将存放在 ~/Library/Logs/homeassistant.log


群晖 NAS

以 root 或 admin 用户 SSH 至群晖,输入

cd /volume1/homeassistant

创建文件 homeassistant.confsudo nano homeassistant.conf

复制粘贴

# only start this service after the httpd user process has started
start on started httpd-user

# stop the service gracefully if the runlevel changes to 'reboot'
stop on runlevel [06]

# run the scripts as the 'http' user. Running as root (the default) is a bad ide
#setuid admin

# exec the process. Use fully formed path names so that there is no reliance on
# the 'www' file is a node.js script which starts the foobar application.
exec /bin/sh /volume1/homeassistant/hass-daemon start

Ctrl + XYEnter 保存退出

ln -s homeassistant.conf /etc/init/homeassistant.conf
chmod -r 777 /etc/init/homeassistant.conf

重启 NAS