0

I have a golang API that needs to run 24/7 for a nginx proxy backstream.

I have a script..

#!/usr/bin/bash cd /root/api && go run . 

That runs fine manually.

But /etc/systemd/system/api.service fails to start

.service:

[Unit] Description=A go-lang API for the www frontend [Service] Type = simple ExecStart=/root/api.sh 

systemctl status reports

× 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 
1
  • you have no After specified in [Unit] - perhaps your systemd unit is starting too early Commented Nov 21, 2024 at 22:46

2 Answers 2

0

As Jaromanda says, you should tell systemd when to start this. You should also specify WHERE the go executable is (but rather than provide the path to the exe a better way is to set the PATH in the environment, e.g. Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin" under [Service]).

You should also learn how to check your logs when something goes wrong.

And you should not be installing services under the root home dir, NOR running exposed services as root!

3
  • Its not exposed that's the whole point of the nginx layer. Commented Nov 22, 2024 at 19:16
  • Logs don't provide anything, I know how to check journalctl Commented Nov 22, 2024 at 19:17
  • I start it manually Commented Nov 22, 2024 at 19:17
0

Did you check jounrnalctl logs ?

Running as root is not recommended. if you want to run only as root, then try to mention it specifically in your script & try.

It's better to create a separate user to run the API service. Please add a log file as well to debug, which would make things easier.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.