ExecStart fails in systemd service for Golang webapp

Issue

I’m new to golang and am trying to host a simple website on my server.

I’m on Ubuntu 18.04

My domain root directory is in /var/www/vhosts/mydomain.com/mydomain.com

There I have a main.go file which is rendering a simple Hello World in the browser.

When I go run main.go from this directory, the webpage is working correctly.

Now I’m trying to create a service to keep the webpage available when I close my shell.

For that I created a new service called golangweb.service at /etc/systemd/system.

The content at that file is:

[Unit]
Description = Go Server

[Service]
ExecStart=/var/www/vhosts/mydomain.com/mydomain.com
Type=simple
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

After saving the file I insert the folling commands in order:

sudo systemctl daemon-reload
sudo systemctl enable golangweb.service
sudo systemctl start golangweb.service
sudo systemctl status golangweb.service

When I try to get the status I get the following error (I xed out some data there):

 golangweb.service - Go Server
   Loaded: loaded (/etc/systemd/system/golangweb.service; enabled; vendor preset: enabl
   Active: activating (auto-restart) (Result: exit-code) since Fri xx-xx-xx 23:43:52
  **Process: xx ExecStart=/var/www/vhosts/mydomain.com/mydomain.com (code=exited, sta
 Main PID: xx (code=exited, status=203/EXEC)**

Mai xx xx:xx:xx xxxxx.xxxx.xxxx.systemd[1]: golangweb.service: Failed with re
Warning: Journal has been rotated since unit was started. Log output is incomplete or u
lines 1-8/8 (END)
‚óŹ golangweb.service - Go Server
   Loaded: loaded (/etc/systemd/system/golangweb.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Fri xx-xx-xx xx:xx:xx CEST; 3s ago
  Process: xx ExecStart=/var/www/vhosts/mydomain.com/mydomain.com (code=exited, status=203/EXEC)
 Main PID: xx (code=exited, status=203/EXEC)

Mai xx xx:xx:xx xxxx.xx.xx xx[1]: golangweb.service: Failed with result 'exit-code'.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

does anybody know why this is happening?

Solution

The first argument toExecStart needs to be an executable file. It looks like you’ve set it to a directory. If you were to try typing /var/www/vhosts/mydomain.com/mydomain.com at the shell prompt, you would see similar behavior: you can’t run a directory.

You could set:

WorkingDirectory=/var/www/vhosts/mydomain.com/mydomain.com
ExecStart=/usr/bin/go run main.go

Alternately, you could compile your code (go build), and then set ExecStart to the full path to the compiled binary:

ExecStart=/var/www/vhosts/mydomain.com/mydomain.com/compiledprogramname

Answered By – larsks

Answer Checked By – Candace Johnson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.