Minecraft Linux Servers - Automated Commands using Screen & Cron
The following is for Minecraft Server administrators running Linux.
If you run your Minecraft server through a named screen session, you can develop simple bash scripts that automate commands. This can be everything from giftbag scripts to automated backups.
An example backup script: /home/user/minecraft/backup.sh
#!/bin/sh
echo "Forcing save-all";
screen -x minecraft1 -X stuff `printf "save-off\r"`;
sleep 5s;
screen -x minecraft1 -X stuff `printf "save-all\r"`;
sleep 60s;
day=$(date +%Y%m%d);
archive_file=/home/user/minecraft/Server-${day}.tgz
orig_file=/home/user/minecraft/Server
backup_file=/home/user/minecraft/Server-${day}
echo "Copying world directory..."
cp -R -p ${orig_file} ${backup_file};
sleep 5s;
screen -x minecraft1 -X stuff `printf "save-on\r"`;
echo "Compressing world directory copy..."
tar --posix --preserve-permissions --preserve-order --create --gzip \
--remove-files \
--file="${archive_file}" \
${backup_file}
sleep 5s;
echo "Moving compressed world directory copy to remote backup site..."
mv ${archive_file} /mnt/backups/minecraft/;
echo "Backup complete!"
Edit: Now includes save-off and save-on as recommended by aperson. Thanks!
Once done, you can edit your cron settings: crontab -e
# m h dom mon dow command
0 12 * * * sh /home/user/minecraft/backup.sh
A giftbag script can also be created: giftbag.sh
#!/bin/sh
echo "Time for toys!";
echo "${2}@${1} must have been very good!"
screen -x ${1} -X stuff "give ${2} 259"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 50 64"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 261"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 262 64"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 264 16"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 265 64"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 276"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 277"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 278"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 279"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 310"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 311"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 312"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 313"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 345"`printf "\r"`;
screen -x ${1} -X stuff "give ${2} 347"`printf "\r"`;
screen -x ${1} -X stuff "tell ${2} It looks like the gods are happy with you today..."`printf "\r"`;
echo "Enjoy!"
To use this script, you can run the following:
./giftbag.sh minecraftScreenName minecraftUserId
If you want to be an evil admin: darkness.sh
#!/bin/sh
screen -x minecraft1 -X stuff "time set 18000"`printf "\r"`;
crontab -e
# m h dom mon dow command
0,5,10,15,20,25,30,35,40,45,50,55 * * * * sh /home/user/minecraft/darkness.sh
Or, the short hand version:
# m h dom mon dow command
*/5 * * * * sh /home/user/minecraft/darkness.sh