树莓派Google Assistant SDK开机自启配置求助(使用hotword.py)
Hey Jerome, I’ve been there—systemd services can feel a bit intimidating at first, but once you have the right config file, it’s smooth sailing. Let’s get your hotword.py running automatically every time your Pi boots up.
Step 1: Create the Systemd Service File
First, we’ll make a dedicated service file for your Google Assistant script. Open a terminal and run this command to create/edit the file with nano:
sudo nano /etc/systemd/system/google-assistant-hotword.service
Paste in the following content, making sure to replace the placeholders with your actual details:
[Unit] Description=Google Assistant Hotword Service After=network.target pulseaudio.target Wants=network.target pulseaudio.target [Service] User=pi # Replace with your Raspberry Pi username if not 'pi' ExecStart=/usr/bin/python3 /home/pi/path/to/your/hotword.py # Replace with the FULL path to your hotword.py script Restart=always RestartSec=5 Environment="DISPLAY=:0" # Needed if your script relies on GUI/visual elements (adjust if not) Environment="XAUTHORITY=/home/pi/.Xauthority" # Same as above, adjust username if needed [Install] WantedBy=multi-user.target
Let me quickly break down what each section does:
[Unit]: Tells systemd what this service is, and that it should wait for network and PulseAudio (for audio functionality) to start before launching.[Service]: Defines how the service runs—what user to execute it as, the exact command to start your script, and to restart it automatically if it crashes (super handy for reliability).[Install]: Tells systemd when to trigger this service (multi-user.target means it runs once the Pi is ready for user sessions).
Step 2: Save and Enable the Service
Once you’ve updated the paths and username, save the file in nano by pressing Ctrl+O, hit Enter, then exit with Ctrl+X.
Next, reload systemd to recognize the new service:
sudo systemctl daemon-reload
Enable the service so it starts on boot:
sudo systemctl enable google-assistant-hotword.service
You can start it right away to test without rebooting:
sudo systemctl start google-assistant-hotword.service
Step 3: Verify It’s Working
Check the status of the service to confirm it’s running:
sudo systemctl status google-assistant-hotword.service
If something goes wrong, you can view detailed logs to debug:
journalctl -u google-assistant-hotword.service -f
Quick Tips to Avoid Issues
- Make sure your
hotword.pyscript has executable permissions: runchmod +x /home/pi/path/to/your/hotword.py - Use absolute paths for everything in your script (like paths to credentials files) — relative paths can cause problems when running via systemd.
- If your script uses a virtual environment, update the
ExecStartline to point to the Python executable inside your venv (e.g.,/home/pi/venv/bin/python3instead of/usr/bin/python3).
That should do it! Reboot your Pi to confirm it starts automatically, and you’re all set.
内容的提问来源于stack exchange,提问作者JEROME C

