Search code examples
goglobsystemdgo-templates

Panic; pattern matches no files on VPS service


I'm currently trying to setup my golang application as a service on a Linux VPS. The golang application runs smoothly on my localhost. On the server itself I can start it with go run main.go and the application is callable via the ip address.

Service Configuration:

[Unit]
Description=vfreight

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/root/go/src/VFreight/main

[Install]
WantedBy=multi-user.target

But when I try to run it as a service, the service always failed.

May 13 20:10:31 srv.vfreight.com systemd[1]: Started vfreight.
May 13 20:10:31 srv.vfreight.com main[3167]: panic: html/template: pattern matches no files: `template/*.tmpl`
May 13 20:10:31 srv.vfreight.com main[3167]: goroutine 1 [running]:
May 13 20:10:31 srv.vfreight.com main[3167]: html/template.Must(...)
May 13 20:10:31 srv.vfreight.com main[3167]: /usr/lib/golang/src/html/template/template.go:374
May 13 20:10:31 srv.vfreight.com main[3167]: VFreight/app/controller.init.0()
May 13 20:10:31 srv.vfreight.com main[3167]: /root/go/src/VFreight/app/controller/control.go:21 +0x9e
May 13 20:10:31 srv.vfreight.com systemd[1]: vfreight.service: main process exited, code=exited, status=2/INVALIDARGUMENT
May 13 20:10:31 srv.vfreight.com systemd[1]: Unit vfreight.service entered failed state.
May 13 20:10:31 srv.vfreight.com systemd[1]: vfreight.service failed.
May 13 20:10:36 srv.vfreight.com systemd[1]: vfreight.service holdoff time over, scheduling restart.
May 13 20:10:36 srv.vfreight.com systemd[1]: Stopped vfreight.

func init() { tmpl = template.Must(template.ParseGlob("template/*.tmpl")) }

I tried to change the template/*.tmpl to ../template/*.tmpl but that doesnt worked.

go env

Hopefully someone has an idea to fix this.

Greetings Chris


Solution

  • You need to add WorkingDirectory= to the directory of your application, where the template directory exists.

    From systemd.exec(5):

    If not set, defaults to the root directory when systemd is running as a system instance

    So your main problem is being run at / (root) and ParseGlob is trying to find stuff at /template/*.tmpl.