25Sep/110
cron job to check if minecraft server is running
I wrote a little script that checks if my Minecraft server status is running, and if it's not try to start it up. I run my server on Ubuntu and I use a wrapper I found on a forum. I can't find the post now (it was on http://www.minecraftforums.com), but I will post it in case anyone want to use this solution, seems to work pretty well and has bukkit support.
My script will call the service status, check if the status is 'not running' or 'running', start it if it's not running and mail myself the results. I'm running it every 5 minutes using cron and, for now, it has waken the server 2 times in 24 hours. This is great as some friends play while I'm sleeping, so they won't wake me up in order to reset the server!
My crontab line is:
1 | */5 * * * * /bin/bash /home/minecraft/check.sh >> check.log |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #!/bin/sh # Email config mail_bin=/usr/bin/mail mail_subject="Minecraft server" mail_address="devnulls@gmail.com" mail_file="$(mktemp)" # Example for worlds="mars venus earth" # minecraft_server contains your wrapper script file worlds="mars" minecraft_server="/etc/init.d/minecraft_server" echo $(date) echo "----------------------- MINECRAFT BARKING DAEMON -----------------------" echo "------------------------------------------------------------------------" check_servers(){ for w in $worlds do echo "Checking $w status..." status=$($minecraft_server status $w | grep 'not running') if [ "$status" != "${status/not running/}" ]; then echo "$w is offline, trying to restart it..." echo "$w is offline, trying to restart it..." >> $mail_file $minecraft_server start $w /usr/bin/mail -s $mail_subject $mail_address < $mail_file else echo "$w is online" echo "$w is online" >> $mail_file /usr/bin/mail -s $mail_subject $mail_address < $mail_file fi echo "------------------------------------------------------------------------" done } # MAIN check_servers echo "" echo "" |