只需在启动时运行它们即可。Systemd

只需在启动时运行它们即可。Systemd

Ubuntu 19.04 嗨,我有一个 systemd 错误,我有一个启动服务器.服务文件包含

[Unit]
Description=LaunchServer
After=network.target

[Service]
WorkingDirectory=/home/aborigen/Launcher/LaunchServer/build/libs
Type=notify
User=aborigenserver
Group=sudo
NotifyAccess=all
Restart=always

ExecStart=/usr/bin/screen -DmS launchserver /usr/java/jdk1.8.0_221/bin/java -Xmx128M -javaagent:LaunchServer.jar -jar LaunchServer.jar
ExecStop=/usr/bin/screen -p 0 -S launchserver -X eval 'stuff "stop"\015'
[Install]
WantedBy=multi-user.target

我的行为算法如下:

  1. aborigen@aborigenserver:/etc/systemd/system$sudo nano launch-server.service

从而创建此文件并粘贴上述参数

  1. aborigen@aborigenserver:/etc/systemd/system$sudo systemctl daemon-reload
  2. aborigen@aborigenserver:/etc/systemd/system$sudo systemctl status launch-server.service 并响应:

● launch-server.service - LaunchServer Lost Paradise
已加载:已加载(/etc/systemd/system/launch-server.service;已禁用;供应商预设:已启用)
活动:未活动(已停止)

  1. aborigen@aborigenserver:/etc/systemd/system$sudo systemctl enable launch-server.service

创建符号链接 /etc/systemd/system/multi-user.target.wants/launch-server.service → /etc/systemd/system/launch-server.service。

  1. aborigen@aborigenserver:/etc/systemd/system$sudo systemctl status launch-server.service

● launch-server.service - LaunchServer Lost Paradise
已加载:已加载(/etc/systemd/system/launch-server.service;已启用;供应商预设:已启用) 活动:未活动(已停止)

  1. aborigen@aborigenserver:/etc/systemd/system$sudo systemctl start launch-server.service

launch-server.service 的作业失败,因为该服务未执行其单元配置所需的步骤。有关详细信息,请参阅“systemctl status launch-server.service”和“journalctl -xe”。

  1. aborigen@aborigenserver:/etc/systemd/system$sudo systemctl status launch-server.service

aborigen@aborigenserver:/etc/systemd/system$ sudo systemctl status launch-server.service

● launch-server.service - LaunchServer Lost Paradise
   Loaded: loaded (/etc/systemd/system/launch-server.service; enabled; vendor preset: enabled)
   Active: failed (Result: protocol) since Mon 2019-07-29 12:45:50 EEST; 2h 49min ago
 Main PID: 2496 (code=exited, status=0/SUCCESS)

июл 29 12:45:50 aborigenserver systemd[1]: launch-server.service: Service RestartSec=100ms expired, scheduling restart.
июл 29 12:45:50 aborigenserver systemd[1]: launch-server.service: Scheduled restart job, restart counter is at 5.
июл 29 12:45:50 aborigenserver systemd[1]: Stopped LaunchServer Lost Paradise.
июл 29 12:45:50 aborigenserver systemd[1]: launch-server.service: Start request repeated too quickly.
июл 29 12:45:50 aborigenserver systemd[1]: launch-server.service: Failed with result 'protocol'.
июл 29 12:45:50 aborigenserver systemd[1]: Failed to start LaunchServer Lost Paradise.
aborigen@aborigenserver:/etc/systemd/system$ chmod +x /home/aborigen/Server/start.sh
aborigen@aborigenserver:/etc/systemd/system$ sudo systemctl start start-server.service
aborigen@aborigenserver:/etc/systemd/system$ sudo systemctl reload start-server.service
Failed to reload start-server.service: Job type reload is not applicable for unit start-server.service.
  1. aborigen@aborigenserver:/etc/systemd/system$journalctl -xe

最终:

-- Назначен автоматический перезапуск юнита launch-server.service, так как для него был задан
-- параметр Restart=.
июл 29 12:45:50 aborigenserver systemd[1]: Stopped LaunchServer Lost Paradise.
-- Subject: Завершена остановка юнита launch-server.service.
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Процесс остановки юнита launch-server.service был завершен.
июл 29 12:45:50 aborigenserver systemd[1]: launch-server.service: Start request repeated too quickly.
июл 29 12:45:50 aborigenserver systemd[1]: launch-server.service: Failed with result 'protocol'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- The unit launch-server.service has entered the 'failed' state with result 'protocol'.
июл 29 12:45:50 aborigenserver systemd[1]: Failed to start LaunchServer Lost Paradise.
-- Subject: Ошибка юнита launch-server.service
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Произошел сбой юнита launch-server.service.
--
-- Результат: failed.

我也有start-server.service

[Unit]
Description=Lost Paradise Server
After=network.target
After=launch-server.service
[Service]
WorkingDirectory=/home/aborigen/Server/

User=aborigenserver
Group=sudo

Restart=always

ExecStart=/home/aborigen/Server/start.sh
ExecStop=/usr/bin/screen -p 0 -S ServerAstrey -X eval 'stuff "stop"\015'
[Install]
WantedBy=multi-user.target

因此:

aborigen@aborigenserver:/etc/systemd/system$ sudosystemctl status start-server.service

● start-server.service - Lost Paradise Server
Loaded: loaded (/etc/systemd/system/start-server.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2019-07-29 15:39:26 EEST; 20min ago
Process: 3427 ExecStart=/home/aborigen/Server/start.sh (code=exited, status=1/FAILURE)
Main PID: 3427 (code=exited, status=1/FAILURE)

июл 29 15:39:26 aborigenserver systemd[1]: start-server.service: Service RestartSec=100ms expired, scheduling restart.
июл 29 15:39:26 aborigenserver systemd[1]: start-server.service: Scheduled restart job, restart counter is at 5.
июл 29 15:39:26 aborigenserver systemd[1]: Stopped Lost Paradise Server.
июл 29 15:39:26 aborigenserver systemd[1]: start-server.service: Start request repeated too quickly.
июл 29 15:39:26 aborigenserver systemd[1]: start-server.service: Failed with result 'exit-code'.
июл 29 15:39:26 aborigenserver systemd[1]: Failed to start Lost Paradise Server.

第二天我就把头发拔掉了。我只需要在启动时运行它们。请帮忙 :)

答案1

您有Active: failed (Result: protocol)(code=exited, status=0/SUCCESS),这表明您正在 systemd exec 命令中运行后台进程。

如果你screen在控制台上运行命令,它会将自己置于后台吗?这应该可以解释。

尝试使用Type=forking,应该可以解决问题。

或者,在前台运行你的screen命令,尽管“附加”可能无法按预期工作,因为 systemd 单元在没有终端的情况下运行,所以屏幕可能会失败......也许是Type=forking解决这个问题的最好方法......

答案2

首先重新检查终端中是否正在运行完全相同的命令。然后记住,如果服务失败,它有时不会立即重新启动,而是等待一段时间。因此每隔几秒钟重新启动会带来麻烦。如果在一定时间内出现太多故障,服务将永久停止。

所有这些都是在 Unit 部分中配置的,我相信你已经知道了。因此,我将为你提供一个工作 Unit 部分的示例,其中包含一些惊喜如果你仔细观察的话。

[Unit]
Description=Radmon service
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=30
User=marc
ExecStart=/usr/bin/python -u /home/marc/GMC/pyradmon.py
StandardOutput=null
StandardError=inherit

[Install]
WantedBy=multi-user.target

我确信这会有所帮助。(提示!Sec=)

相关内容