我正在尝试使用 systemd 运行 minecraft 服务器,但它不起作用。这是一个简单的测试文件,为简单起见,仅替换了“execstart”和“execstop”位。
root@Paidia:~# systemctl start test@one
root@Paidia:~# systemctl status test@one
● [email protected] - Test one
Loaded: loaded (/etc/systemd/system/[email protected]; disabled; vendor preset: enabled)
Active: inactive (dead)
Oct 31 23:03:21 Paidia echo[398]: I started
Oct 31 23:03:21 Paidia echo[399]: I stopped
Oct 31 23:03:21 Paidia systemd[1]: Started Test one.
root@Paidia:~# cat /etc/systemd/system/test\@.service
[Unit]
Description=Test %i
[Service]
Type=forking
ExecStart=/bin/echo "I started"
ExecStop=/bin/echo "I stopped"
[Install]
WantedBy=multi-user.target
使用实际代码进行编辑
root@Paidia:~# cat /etc/systemd/system/minecraft\@.service
[Unit]
Description=Minecraft Server %i
[Service]
WorkingDirectory=/opt/minecraft-%i
User=minecraft
Type=forking
ExecStart=/usr/bin/screen -DmS mc-%i /bin/java -Xmx2048M -jar minecraft_server.jar nogui
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."\\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\\015'
ExecStop=/bin/sleep 2
[Install]
WantedBy=multi-user.target
root@Paidia:~# systemctl start minecraft@survival
Job for [email protected] failed because the control process exited with error code. See "systemctl status [email protected]" and "journalctl -xe" for details.
root@Paidia:~# systemctl status minecraft@survival
● [email protected] - Minecraft Server survival
Loaded: loaded (/etc/systemd/system/[email protected]; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2017-11-01 01:32:44 UTC; 8s ago
Process: 422 ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval stuff "say SERVER SHUTTING DOWN. Saving map..."\015 (code=exited, status=1/FAILURE)
Process: 420 ExecStart=/usr/bin/screen -DmS mc-%i /bin/java -Xmx2048M -jar minecraft_server.jar nogui (code=exited, status=0/SUCCESS)
Nov 01 01:32:43 Paidia systemd[1]: Starting Minecraft Server survival...
Nov 01 01:32:44 Paidia systemd[1]: [email protected]: Control process exited, code=exited status=1
Nov 01 01:32:44 Paidia systemd[1]: Failed to start Minecraft Server survival.
Nov 01 01:32:44 Paidia systemd[1]: [email protected]: Unit entered failed state.
Nov 01 01:32:44 Paidia systemd[1]: [email protected]: Failed with result 'exit-code'.
root@Paidia:~# journalctl -u minecraft@survival
-- Logs begin at Wed 2017-11-01 01:32:10 UTC, end at Wed 2017-11-01 01:32:44 UTC. --
Nov 01 01:32:43 Paidia systemd[1]: Starting Minecraft Server survival...
Nov 01 01:32:44 Paidia systemd[1]: [email protected]: Control process exited, code=exited status=1
Nov 01 01:32:44 Paidia systemd[1]: Failed to start Minecraft Server survival.
Nov 01 01:32:44 Paidia systemd[1]: [email protected]: Unit entered failed state.
Nov 01 01:32:44 Paidia systemd[1]: [email protected]: Failed with result 'exit-code'.
如您所见,它尝试同时执行“ExecStart”和“ExecStop”。
答案1
“控制进程已退出,代码=已退出状态=1”表示服务启动的主进程以状态 1 退出,没有任何幸存的分叉进程。由于您使用了screen -Dm
,这意味着screen
当命令退出时退出。-dm
改为使用 保持screen
活动状态,然后重新连接它以查看发生了什么。
答案2
我遇到了类似的问题,发现屏幕屏蔽了 Java 的错误代码。当我单独运行此命令时:/bin/java -Xmx2048M -jar minecraft_server.jar nogui 我收到一个错误,提示 minecraft 是由比我系统上安装的 Java Runtime 版本更新的版本编译的(60 vs 55)