Search code examples
linuxubuntugoservicedaemon

How to make my app run in background in Ubuntu


I created a service in my Ubuntu instance by adding it to systemd like:

sudo systemctl enable myservice.service

Created symlink /etc/systemd/system/multi-user.target.wants/myservice.service → /etc/systemd/system/myservice.service. 

the setting inside my myservice.service are:

[Unit]
Description=myserviceService

[Service]
Restart=always
Type=simple
ExecStart=/home/myservice-app/core/core

[Install]
WantedBy=multi-user.target

This is the error when I check status:

Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Main process exited, code=exited, status=1/FAILURE
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Failed with result 'exit-code'.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Scheduled restart job, restart counter is at 5.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: Stopped myserviceService.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Start request repeated too quickly.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Failed with result 'exit-code'.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: Failed to start myservice Service.

What am I doing wrong? The service runs properly in foreground with no errors, its just the service that struggles to run.


Solution

  • If your Go program is able to start normally (meaning not as a service, but manually from command line), then check if this is a policy issue.

    For instance, a SELinux policy could prevent your Go binary to start if it is not installed in a system path (like /usr/local/bin).
    Or the service definition uses relative instead of absolute paths (same here).
    Or with the wrong user.