Skip to content

Instantly share code, notes, and snippets.

@Albirew Albirew/pocketmine.sh
Last active Mar 4, 2018

Embed
What would you like to do?
PocketMine (Minecraft Pocket Edition) server init start script using SCREEN
#!/bin/bash
# /etc/init.d/pocketmine
### BEGIN INIT INFO
# Provides: pocketmine
# Required-Start: $local_fs $network $remote_fs screen-cleanup
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PocketMine server
# Description: Start PocketMine (Minecraft Pocket Edition) server using a SCREEN session.
### END INIT INFO
# Installation:
# chmod 755 pocketmine.sh
# sudo cp pocketmine.sh /etc/init.d/pocketmine
# sudo chown root:root /etc/init.d/pocketmine
# sudo update-rc.d pocketmine defaults
# sudo update-rc.d pocketmine enable
#Settings
SERVICE='PocketMine-MP'
USERNAME="pocketmine"
MCPATH='/home/pocketmine/minecraft-bedrock/'
BACKUPPATH='/home/pocketmine/backup/'
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 -v -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 pocketmine bash start.sh"
sleep 7
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is now running."
else
echo "Could not start $SERVICE."
fi
fi
}
mc_saveoff() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... suspending saves"
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly...\"\015'"
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"save-off\"\015'"
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"save-all\"\015'"
sync
sleep 10
else
echo "$SERVICE was not running. Not suspending saves."
fi
}
mc_saveon() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... re-enabling saves"
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"save-on\"\015'"
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
else
echo "$SERVICE was not running. Not resuming saves."
fi
}
mc_stop() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running... stopping."
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"save-all\"\015'"
sleep 10
as_user "screen -p 0 -S pocketmine -X eval 'stuff \"stop\"\015'"
sleep 7
else
echo "$SERVICE was not running."
fi
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE could not be shut down... still running."
else
echo "$SERVICE is shut down."
fi
}
mc_backup() {
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
# echo "$SERVICE is running! Will not start backup."
# else
echo "Backing up pocketmine data"
if [ -d $BACKUPPATH/worlds_`date "+%Y.%m.%d"` ]
then
for i in 1 2 3 4 5 6
do
if [ -d $BACKUPPATH/worlds_`date "+%Y.%m.%d"`-$i ]
then
continue
else
as_user "cd $MCPATH && cp -r worlds $BACKUPPATH/worlds_`date "+%Y.%m.%d"`-$i"
break
fi
done
else
as_user "cd $MCPATH && cp -r worlds $BACKUPPATH/worlds_`date "+%Y.%m.%d"`"
fi
echo "Backing up the pocketmine server executable"
if [ -f "$BACKUPPATH/pocketmine_server_`date "+%Y.%m.%d"`.jar" ]
then
for i in 1 2 3 4 5 6
do
if [ -f "$BACKUPPATH/pocketmine_server_`date "+%Y.%m.%d"`-$i.jar" ]
then
continue
else
as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/pocketmine_server_`date "+%Y.%m.%d"`-$i.jar\""
break
fi
done
else
as_user "cd $MCPATH && cp minecraft_server.jar \"$BACKUPPATH/pocketmine_server_`date "+%Y.%m.%d"`.jar\""
fi
fi
echo "Backup complete"
}
#Start-Stop here
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
mc_stop
mc_start
;;
backup)
mc_saveoff
mc_backup
mc_saveon
;;
status)
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
*)
echo "Usage: /etc/init.d/pocketmine {start|stop|backup|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
You can’t perform that action at this time.