Last active
September 26, 2019 23:49
-
-
Save gamerlv/4638046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*/5 * * * * service multicraft sync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: multicraft | |
# Required-Start: $remote_fs $syslog | |
# Required-Stop: $remote_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start the multicraft daemon | |
# Description: Start ramdisks and the multicraft daemon | |
### END INIT INFO | |
# /etc/init.d/multicraft | |
USERNAME='minecraft' | |
MC_HOME="/home/minecraft/multicraft" | |
MC_PARAMS="-v" | |
MC_BIN="bin" | |
MC="$MC_HOME/$MC_BIN/multicraft" | |
RD_SIZE="8G" #G=giga, t=terra,m=mega,k=kilo, nothing=byte | |
RD_backingStore="$MC_HOME/servers_backup/" | |
RD="$MC_HOME/servers/" | |
ME=`whoami` | |
as_user() { | |
if [ $ME == $USERNAME ] ; then | |
bash -c "$1" | |
else | |
su - $USERNAME -c "$1" | |
fi | |
} | |
syncToDisk(){ | |
if [ -d $RD ]; then | |
echo "Syncing RAMDISK back to disk"; | |
cd $MC_HOME; | |
rsync -r -t -og --perms --force "$RD" "$RD_backingStore"; | |
echo "Done syncing."; | |
else | |
echo "Whoops Can't syn to disk. RAMDISK not started!" >&2; | |
fi | |
} | |
case "$1" in | |
start) | |
if [ -d "$RD" ] && [ "$(ls -A $RD)" ]; then | |
mv "$RD" "$RD_backingStore"; | |
fi | |
if [ ! -d "$RD" ]; then | |
# This almost should never happen | |
as_user "mkdir -p $RD"; | |
fi | |
mountpoint -q "$RD" || mount -t tmpfs -o size=$RD_SIZE tmpfs "$RD" | |
rsync -r -t -og --perms --force "$RD_backingStore" "$RD"; #seed the ramdisk | |
echo "Starting Multicraft" | |
"$MC" $MC_PARAMS start | |
;; | |
stop) | |
echo "Stopping Multicraft" | |
"$MC" $MC_PARAMS stop | |
echo "Syncing Multicraft" | |
syncToDisk | |
sync | |
;; | |
restart) | |
echo "Restarting Multicraft" | |
"$MC" $MC_PARAMS restart | |
;; | |
sync) | |
echo "Syncing Multicraft" | |
if [ -d "$RD" ] && [ "$(ls -A $RD)" ]; then | |
syncToDisk | |
sync | |
fi | |
;; | |
*) | |
echo "Usage: /etc/init.d/multicraft {start|stop|restart|sync}" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment