Skip to content

Commit 4b4e9d5

Browse files
committed
initial commiot
0 parents  commit 4b4e9d5

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Linux Unit File for Minecraft Server
2+
3+
This is a unit file to stop and start Minecraft server using systemd.
4+
5+
This file must be placed in `/etc/systemd/system`
6+
7+
Please modify the **Description** and **WorkingDirectory** to use this unit file.
8+
9+
> **For those who are not familiar with unit file, here are some of the additional information**
10+
>
11+
> Description=xxxxx is a description that shown when running `sudo systemctl status minecraft.service`
12+
>
13+
> WorkingDirectory=xxxx is the folder that the server is located, recommanded to put it inside /opt/ or /srv/ (eg /opt/minecraft_server)
14+
>
15+
> Whenever you modified the file, please run `sudo systemctl daemon-reload` to update the systemd
16+
17+
---
18+
19+
To use this script, user named `minecraft` must present in the server. To create it please run `sudo useradd -s /bin/bash -m minecraft`, then `sudo passwd minecraft` to assign a password to the user.
20+
21+
Screen command must also need to be installed. To install it on Ubuntu, please run `sudo apt install screen`
22+
23+
A `start.sh` bash script is recommended, you can generate it in here: [Start Script Generator | PaperMC Docs](https://docs.papermc.io/misc/tools/start-script-gen)
24+
25+
In this unit file, it will automatically execute the `./start.sh` file located in the in the root directory of the Minecraft server using a screen instance as the user `minecraft`.
26+
27+
---
28+
29+
**To enable the auto-start of the server during the boot process, please run**
30+
`sudo systemctl enable minecraft.service`
31+
32+
**To disable the auto-start of the server during the boot process, please run**
33+
`sudo systemctl disable minecraft.service`
34+
35+
**To start the server manually, please run**
36+
`sudo systemctl start minecraft.service`
37+
38+
**To stop the server manually, please run**
39+
`sudo systemctl stop minecraft.service`
40+
41+
---
42+
43+
For more information regarding systemd and unit file, please refer to this article [systemd - ArchWiki (archlinux.org)](https://wiki.archlinux.org/title/systemd)

minecraft.service

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[Unit]
2+
Description=A Minecraft Server
3+
After=network-online.target
4+
5+
[Service]
6+
WorkingDirectory=/opt/minecraft_server
7+
Type=forking
8+
9+
# Choosing which user to run the process
10+
PrivateUsers=true
11+
12+
User=minecraft
13+
Group=minecraft
14+
15+
# Hardening
16+
ProtectSystem=full
17+
ProtectHome=true
18+
ProtectKernelTunables=true
19+
ProtectKernelModules=true
20+
ProtectControlGroups=true
21+
22+
# Starting Minecraft Server
23+
ExecStart=/usr/bin/screen -dmS minecraft-server ./start.sh
24+
25+
# Stopping Minecraft Server
26+
ExecStop=/usr/bin/screen -p 0 -S minecraft-server -X stuff 'kick @a Server Maintance. Please check back later. ^M'
27+
ExecStop=/usr/bin/screen -p 0 -S minecraft-server -X stuff 'save-all ^M'
28+
ExecStop=/usr/bin/screen -p 0 -S minecraft-server -X stuff 'stop ^M'
29+
ExecStop=/bin/bash -c 'while /usr/bin/screen -list | grep -q "minecraft-server"; do sleep 1; done'
30+
31+
# Handle failure
32+
Restart=on-failure
33+
RestartSec=60s
34+
35+
[Install]
36+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)