Improving organization in the event that I add more scripts to this repo
This commit is contained in:
25
minecraft/minecraft.service
Normal file
25
minecraft/minecraft.service
Normal file
@ -0,0 +1,25 @@
|
||||
[Unit]
|
||||
Description=Minecraft Server
|
||||
|
||||
[Service]
|
||||
|
||||
Type=forking
|
||||
|
||||
User=minecraft
|
||||
Group=minecraft
|
||||
WorkingDirectory=/opt/minecraft
|
||||
|
||||
ExecStartPre=/opt/minecraft/scripts/rdify --init /opt/minecraft/world
|
||||
ExecStart=/opt/minecraft/scripts/minecraft/mineserve start
|
||||
ExecStartPost=screen -S mine_world_sync -dm /opt/minecraft/scripts/rdify --loop_sync 15 /opt/minecraft/world
|
||||
|
||||
ExecStop=screen -S mine_world_sync -X stuff ""
|
||||
ExecStop=/opt/minecraft/scripts/minecraft/mineserve stop
|
||||
ExecStopPost=/opt/minecraft/scripts/rdify --sync /opt/minecraft/world
|
||||
ExecStopPost=/opt/minecraft/scripts/rdify --cleanup /opt/minecraft/world
|
||||
|
||||
Restart=on-failure
|
||||
RestartSec=30
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
66
minecraft/mineserve
Executable file
66
minecraft/mineserve
Executable file
@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
|
||||
PID=/opt/minecraft/mc.pid
|
||||
|
||||
status() {
|
||||
screen -S minecraft -X stuff "list
|
||||
"
|
||||
echo
|
||||
echo "== Status =="
|
||||
ps -p $(cat $PID) -o %cpu,%mem,cmd
|
||||
echo
|
||||
LAST_STATUS=$(cat logs/latest.log | grep -n "players online:" | tail -n1 | cut -d ":" -f 1)
|
||||
tail -n +$LAST_STATUS logs/latest.log
|
||||
echo
|
||||
}
|
||||
|
||||
start() {
|
||||
if test -f "$PID"; then
|
||||
echo "Server already running with pid: $(cat $PID)"
|
||||
status
|
||||
exit 0
|
||||
fi
|
||||
screen -S minecraft -dm java -Xms1G -Xmx5G -jar server.jar nogui
|
||||
pgrep -P "$(screen -list | sed -E -n 's/\s*([0-9]+).minecraft.*/\1/p')" > $PID
|
||||
}
|
||||
|
||||
stop() {
|
||||
if test -f "$PID"; then
|
||||
SCREEN_PID=$(screen -list | sed -E -n 's/\s*([0-9]+).minecraft.*/\1/p')
|
||||
screen -S minecraft -X stuff "stop
|
||||
"
|
||||
echo -n "Waiting for shutdown..."
|
||||
# kill -0 checks if a process is running
|
||||
while kill -0 $SCREEN_PID 2> /dev/null; do
|
||||
sleep 3
|
||||
echo -n "."
|
||||
done
|
||||
echo " Shutdown Complete!"
|
||||
rm $PID
|
||||
exit 0
|
||||
fi
|
||||
echo "No server is running..."
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
'start')
|
||||
start
|
||||
;;
|
||||
'stop')
|
||||
stop
|
||||
;;
|
||||
'restart')
|
||||
stop
|
||||
start
|
||||
;;
|
||||
'status')
|
||||
status
|
||||
;;
|
||||
*)
|
||||
echo
|
||||
echo "Usage: $0 { start | stop | restart | status }"
|
||||
echo
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
Reference in New Issue
Block a user