You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Systemd启动与手动启动.NET API监听地址不一致问题排查

问题解决:树莓派上.NET API作为systemd服务启动时监听地址异常

核心原因

手动启动时程序在项目目录下运行,能正确加载appsettings.json;但systemd默认以根目录/作为工作目录,导致程序找不到配置文件, fallback到.NET默认的http://localhost:5000监听地址。

修复方案

修改你的systemd服务配置文件/lib/systemd/system/<app>.service,补充以下关键配置:

方案1:指定工作目录(推荐)

在[Service]段添加WorkingDirectory,指向可执行文件所在的目录:

[Unit]
Description=Lauches API on boot
After=multi-user.target

[Service]
ExecStart=/home/<user>/Dev/<app_executable>
User=<user>
# 指定程序运行的工作目录,确保能找到appsettings.json
WorkingDirectory=/home/<user>/Dev/

[Install]
WantedBy=multi-user.target

方案2:强制设置环境变量(兜底)

如果方案1不生效,可以直接通过环境变量指定监听地址,覆盖配置文件:

[Service]
ExecStart=/home/<user>/Dev/<app_executable>
User=<user>
WorkingDirectory=/home/<user>/Dev/
# 强制设置监听地址,替换为你的树莓派IP和自定义端口
Environment="ASPNETCORE_URLS=http://<raspberry_ip>:<custom-port>"

生效步骤

修改配置后执行以下命令:

  1. 重新加载systemd配置:sudo systemctl daemon-reload
  2. 重启服务:sudo systemctl restart <app>.service
  3. 验证服务状态:sudo systemctl status <app>.service
  4. 查看运行日志(排查问题用):journalctl -u <app>.service -f

内容的提问来源于stack exchange,提问作者Erwann Suard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 09:15:01