Search code examples
godaemonsystemd

Cannot run go binary as systemctl daemon


I have a go web app which is on the path /home/me/go/src/myapp. When I run the executable using ./myapp on bash terminal, it works fine. However this requires an open terminal to continue running, which is not practial so I tried to make a systemd daemon on my Debian server's /etc/systemd/system/myapp.service like this:

[Unit]
Description=MyApp Daemon
StartLimitIntervalSec=0
[Service]
Type=simple
User= me
Group=www-data
ExecStart=/home/me/go/src/myapp/myapp
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target

I have enabled and started the daemon:

systemctl enable myapp

Start it:

systemctl start myapp

However it fails to run the daemn, and I get this error:

# systemctl status myapp
● myapp.service - MyApp Daemon
   Loaded: loaded (/etc/systemd/system/myapp.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-07-17 05:42:18 CDT; 4s ago
  Process: 19058 ExecStart=/home/me/go/src/myapp/myapp (code=exited, status=127)
 Main PID: 19058 (code=exited, status=127)
      CPU: 2ms
 Jul 17 05:42:18 front systemd[1]: Started Myapp Daemon.
Jul 17 05:42:18 front systemd[1]: myapp.service: Main process exited, code=exited, status=127/n/a
Jul 17 05:42:18 front systemd[1]: myapp.service: Unit entered failed state.
Jul 17 05:42:18 front systemd[1]: myapp.service: Failed with result 'exit-code'.

I'm wondering what could be wrong and how should I fix it?


Solution

  • After lots of trial and error this config worked for me:

    [Unit]
    Description=Sai Go webapp Daemon
    #After=network.target
    StartLimitIntervalSec=0
    [Service]
    Type=simple
    User= me
    Group=www-data
    WorkingDirectory=/home/me/go/src/myapp/
    ExecStart=/home/me/go/src/myapp/myapp
    TimeoutStopSec=300
    [Install]
    WantedBy=multi-user.target
    

    Apparently WorkingDirectory was necessary.