systemd无法启动手动执行正常的Go API启动脚本问题求助
systemd无法启动手动执行正常的Go API启动脚本问题求助
各位大佬好,我碰到个头疼的问题:我有个需要7*24小时运行的Go语言API服务,作为Nginx反向代理的后端使用。手动运行启动脚本完全正常,但用systemd配置的service文件启动就直接失败了,想请大家帮忙分析下问题出在哪。
先给大家看一下相关的配置和错误信息:
手动启动脚本(执行正常)
脚本路径是/root/api.sh,内容如下:
#!/usr/bin/bash cd /root/api && go run .
我直接在终端里执行这个脚本,API服务能正常启动运行,完全没毛病。
systemd服务配置文件
我在/etc/systemd/system/api.service里写了如下配置:
[Unit] Description=A go-lang API for the www frontend [Service] Type = simple ExecStart=/root/api.sh
systemctl status的错误输出
执行systemctl status api.service得到的错误信息如下:
× api.service - A go-lang API for the www frontend Loaded: loaded (/etc/systemd/system/api.service; static) Active: failed (Result: exit-code) since Thu 2024-11-21 18:55:03 UTC; 2min 17s ago Duration: 118ms Invocation: e6e602afee2b4bf6b2d623c15fcbb65e Process: 343565 ExecStart=/root/api.sh (code=exited, status=1/FAILURE) Main PID: 343565 (code=exited, status=1/FAILURE) Mem peak: 2.8M CPU: 50ms
我实在搞不明白,为什么手动执行脚本一切正常,换成systemd启动就直接失败了?有没有大佬能给点排查思路或者指出可能的问题点呀?
备注:内容来源于stack exchange,提问作者Stoic Zebra




