Question

Problem with a Golang webapp and system service

Hi, I am trying to create a Golang server using the gin framework on ubuntu. It works fine when it is executed in the terminal after building it with go build and equally works well locally.

Systemd Description=goapp

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/home/.../goapp/main


[Install]
WantedBy=multi-user.target

I got this error

goapp.service - rediate
   Loaded: loaded (/lib/systemd/system/goapp.service; disabled; 
vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Thu 
2022-09-29 08:14:10 UTC; 66ms ago
   Process: 21628 
ExecStart=/home/.../go/goapp/main (code=exited, status=2)
Main PID: 21628 (code=exited, status=2)
    CPU: 9ms

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 2, 2022
Accepted Answer

Hi there,

If you run go run . inside /home/.../goapp/ does it work as expected?

If this works, what I could suggest is updating the unit file to:

[Unit]
Description=Your Go App
ConditionPathExists=/home/.../goapp
After=network.target

[Service]
Type=simple
User=burn
Group=burn

WorkingDirectory=/home/.../goapp
ExecStart=/usr/local/go/bin/go run .

Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=appgoservice

[Install]
WantedBy=multi-user.target

If this still fails, you should be able to see some output in the syslog. Feel free to share the errors here so I could try to advise you further!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.