Skip to content

Instantly share code, notes, and snippets.

@growtopiajaw
Created November 3, 2019 10:57
Show Gist options
  • Save growtopiajaw/721d0c8ee1321931dd0d1dada9416e1c to your computer and use it in GitHub Desktop.
Save growtopiajaw/721d0c8ee1321931dd0d1dada9416e1c to your computer and use it in GitHub Desktop.

chmod +x /etc/init.d/mcmyadmin

update-rc.d mcmyadmin defaults

#!/bin/bash
# /etc/init.d/mcmyadmin
# set perms to 755
# Derived from script here: http://blog.bigdinosaur.org/serving-minecraft-on-ubuntu/
# and here: http://forums.srcds.com/viewtopic/5835
# reddit.com/u/dicknuckle
# depends: mono mcmyadmin minecraft
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft server
# Description: Starts the minecraft server
### END INIT INFO
#Settings
SERVICE='minecraft'
USERNAME="minecraft"
MCPATH='/root/McMyAdmin'
## If you have more than one Minecraft instance to control, add additional MCPATH
## variables below
# MCPATH2='/home/mcuser/minecraft2'
ME=`whoami`
as_user() {
if [ "$ME" == "$USERNAME" ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
mc_start() {
if ps ax | grep -v grep | grep -i SCREEN | grep $SERVICE > /dev/null
then
echo "Tried to start but $SERVICE was already running!"
else
echo "$SERVICE was not running... starting."
cd $MCPATH
as_user "cd $MCPATH && screen -dmS minecraft ./MCMA2_Linux_x86_64"
## Add additional lines for additioanl Minecrat instances below here
# as_user "cd $MCPATH2 && screen -dmS minecraft ./MCMA2_Linux_x86_64"
sleep 7
if ps ax | grep -v grep | grep -i SCREEN | grep minecraft > /dev/null 2>&1
then
echo "$SERVICE is now running."
else
echo "Could not start $SERVICE."
fi
fi
}
mc_stop() {
if ps ax | grep -v grep | grep -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... stopping."
## Add a line for each task for each Minecraft server you're controlling
# wake the server (not workig right now, so dont use auto sleep function in McMyAdmin)
# as_user "screen -p 0 -S minecraft -X eval 'stuff \"/start\"\015'"
sleep 7
# broadcast shutdown notification to players
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
# save the world
as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'"
sleep 10
# send McMyAdmin console the /quit command
as_user "screen -p 0 -S minecraft -X eval 'stuff \"/quit\"\015'"
sleep 7
else
echo "$SERVICE was not running."
fi
if ps ax | grep -v grep | grep -i SCREEN | grep minecraft > /dev/null
then
echo "$SERVICE could not be shut down... still running."
else
echo "$SERVICE is shut down."
fi
}
################### McMyAdmin /update FUNCTION DOES NOT WORK CRASHES SERVER!!!!!!!!!
mc_update() {
as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER GOING DOWN FOR UPDATE IN 10 SECS!\"\015'"
sleep 10
as_user "screen -p 0 -S minecraft -X eval 'stuff \"/update\"\015'"
echo "Sent /update command to $SERVICE"
}
service_status() {
if ps ax | grep -v grep | grep -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
}
# Start-Stop here
case "$1" in
'start')
mc_start
;;
'stop')
mc_stop
;;
'restart')
mc_stop
sleep 1
mc_start
;;
'status')
service_status
;;
'update')
mc_update
;;
*)
# echo "Usage: /etc/init.d/mcmyadmin {start|stop|update|status|restart}"
echo "Usage: /etc/init.d/mcmyadmin {start|stop|status|restart}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment