Bukkit服务器我一直用GNU Screen跑,隔两天有时候会不稳定要重启。好麻烦根本就不关心为什么,搞定就行。有bukkit插件可以实现但是咱多没自尊用那个。虽然感觉bash操作Screen也挺邋遢的.. 但是好用!其实不然的话怎么操作后台,nohup倒腾输入流什么的太麻烦了,这种用途还是terminal模拟器好看。
[bash]
#!/bin/bash
#/home/bukkit/restart.sh
i=0
while (( $i < 5 ))
do
if [ "`ps aux|grep -e ‘^bukkit’|grep SCREEN`" != "" ]; then
su bukkit -c ‘screen -S bukkit -X stuff ‘stop`echo -ne ‘\015’`” #`echo -ne ‘\015’` 等于敲下回车
echo "`date` restart.sh:" ‘Stoping bukkit…’
((i+=1))
sleep 5
else
((i+=100))
fi
done
if (( $i == 100 )); then
echo "`date` restart.sh:" ‘Bukkit was not running. Starting…’
fi
if (( $i == 5 )); then
kill `ps aux|grep bukkit|awk ‘{print $2}’`
echo "`date` restart.sh:" ‘Forcibly killing bukkit…’
sleep 1
fi
if [ "`ps aux|grep -e ‘^bukkit’|grep SCREEN`" != "" ]; then
echo "`date` restart.sh:" "ERROR: Couldn’t stop bukkit. Exiting."
exit 1
fi
bash /home/bukkit/start.sh
exit 0
[/bash][bash]
#!/bin/bash
#/home/bukkit/start.sh
if [ "`ps aux|grep -e ‘^bukkit’|grep SCREEN`" == "" ]; then
echo `date` start.sh: "Starting bukkit…"
su bukkit -c "screen -dmS bukkit java -Xincgc -Xmx1000m -jar /home/bukkit/craftbukkit-1.6.4-R2.0.jar"
echo `date` start.sh: "Done."
# else
# echo `date` start.sh: "Bukkit is already running."
fi
exit 0
[/bash]
加到cron里就好,第一行是因为有时候服务器会挂掉..
*/5 * * * * bash /home/bukkit/start.sh >> /home/bukkit/cron.log
0 0 * * * bash /home/bukkit/restart.sh >> /home/bukkit/cron.log
如果不把输出导走会给你一堆mails
The `screen -X stuff` trick with `echo -ne ‘\015’` to send the Enter key is a neat detail — that carriage return is the part people usually forget when piping commands into a Screen session. The loop counter doubling from 5 to 100 as a flag for “was never running” is a bit cryptic, but it gets the job done without extra state. When I want a break from shell wrangling, I mess around on krillion.me, a browser party word game.
The loop that retries `screen -X stuff ‘stop’` up to five times before falling back to `kill` is a neat way to handle a server that ignores the first stop command. Using `echo -ne ‘\015’` to send the carriage return into the screen session is a small detail I hadn’t seen before, and it makes the whole thing feel less like a hack. When I want a break from shell tinkering, I usually end up on mimicparty.me for a quick round of bluffing with friends.
The `screen -S bukkit -X stuff` trick with the `echo -ne ‘\015’` to simulate the Enter key is a neat detail — that carriage return is easy to overlook, and without it the stop command just sits there. The loop counting to 5 before force-killing is a sensible fallback too, since Bukkit doesn’t always shut down cleanly. I’ve been poking at a daily word-guessing puzzle called noillirk.app (noillirk.app) as a small break from server babysitting.
文中关于「Bukkit服务器我一直用GNU Screen跑,隔两天有时候会不稳定要重启。好」这一点比较具体,比空泛总结更有用。另外提一下,baperrasta.app 是个免费达卡开车浏览器游戏,适合歇一口气时打开。