ExecStart在Golang Web应用的systemd服务中失败。

huangapple go评论81阅读模式
英文:

ExecStart fails in systemd service for Golang webapp

问题

我是新手,正在尝试在我的服务器上托管一个简单的网站。

我使用的是Ubuntu 18.04。

我的域名根目录在/var/www/vhosts/mydomain.com/mydomain.com

在那里,我有一个main.go文件,在浏览器中呈现一个简单的Hello World。

当我从这个目录运行go run main.go时,网页可以正常工作。

现在我正在尝试创建一个服务,在关闭我的shell时保持网页可用。

为此,我在/etc/systemd/system下创建了一个名为golangweb.service的新服务。

该文件的内容如下:

[Unit]
Description = Go Server

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

[Install]
WantedBy=multi-user.target

保存文件后,我按顺序插入以下命令:

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

当我尝试获取状态时,我收到以下错误(我已经将一些数据打码):

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.

有人知道为什么会发生这种情况吗?

英文:

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?

答案1

得分: 2

ExecStart的第一个参数需要是一个可执行文件。看起来你把它设置成了一个目录。如果你在命令提示符下输入/var/www/vhosts/mydomain.com/mydomain.com,你会看到类似的行为:你不能运行一个目录。

你可以这样设置:

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

或者,你可以编译你的代码(go build),然后将ExecStart设置为编译二进制文件的完整路径:

ExecStart=/var/www/vhosts/mydomain.com/mydomain.com/compiledprogramname
英文:

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

huangapple
  • 本文由 发表于 2022年5月28日 05:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/72411431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定