execstart在Golang WebApp的SystemD服务中失败

发布于 2025-02-02 00:13:22 字数 2007 浏览 5 评论 0原文

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

我在ubuntu 18.04上,

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

我有一个main.go file,在浏览器中渲染一个简单的Hello World。

当i 从此目录中运行main.go时,该网页正常工作。

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

为此,我创建了一个新的服务称为golangweb.service at /etc/etc/systemd/systemd/system

该文件的内容是:

[Unit]
Description = Go Server

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

[Install]
WantedBy=multi-user.target

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2025-02-09 00:13:22

execstart的第一个参数需要是可执行文件。看起来您已将其设置为目录。如果您尝试在Shell提示符下尝试键入/var/www/vhosts/mydomain.com/mydomain.com/mydomain.com,您会看到类似的行为:您无法运行目录。

可以 set:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文