updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / minecraft-server / minecraft-server-rc
blob744c7e196d0951f097432ea8e25c535cbf74ce39
1 #!/bin/bash
3 . /etc/rc.conf
4 . /etc/rc.d/functions
6 _daemonName="Minecraft server"
7 _logDir="/var/log/minecraft-server"
8 _runDir="/var/run/minecraft-server"
9 _srvDir="/srv/minecraft"
10 _expectScript=$(cat << EOS
11 proc exitServer {} {
12 send "stop\r"
13 expect "Saving chunks" {return 0}
14 return 1
17 spawn attachtty "$_runDir/socket"
18 send "save-all\r"
19 expect {
20 "Save complete.$" {
21 exit [exitServer]
23 default {
24 exit [exitServer]
27 EOS
30 case "$1" in
31 start)
32 stat_busy "Starting $_daemonName"
33 if [[ ! -f "$_runDir/server.pid" ]]; then
34 cd "$_srvDir"
35 if [[ ! -d "$_runDir" ]]; then
36 install -m755 -d "$_runDir"
37 chown minecraft:minecraft "$_runDir"
39 su minecraft -c "source /etc/profile &&
40 detachtty --dribble-file '$_logDir/server_stdout.log' \
41 --log-file '$_logDir/detachtty.log' \
42 --pid-file '$_runDir/server.pid' \
43 '$_runDir/socket' '/usr/bin/minecraft-server' nogui" &&
44 chmod 770 "$_runDir/socket" &&
45 chmod 644 "$_runDir/server.pid" &&
46 chown minecraft:log "$_logDir/server_stdout.log" &&
47 chown minecraft:log "$_logDir/detachtty.log" &&
48 chmod 640 "$_logDir/server_stdout.log" &&
49 chmod 640 "$_logDir/detachtty.log" &&
50 add_daemon minecraft-server &&
51 stat_done || stat_fail
52 else
53 stat_fail
56 stop)
57 stat_busy "Stopping $_daemonName"
58 if [[ ! -f "$_runDir/server.pid" ]]; then
59 # server died
60 rm_daemon minecraft-server
61 stat_fail
62 else
63 if [[ -S "$_runDir/socket" ]]; then
64 echo "$_expectScript" | expect - &> /dev/null &&
65 # server stopped
66 rm_daemon minecraft-server &&
67 stat_done ||
68 # server didn't stop
69 stat_fail
70 else
71 # server died
72 rm_daemon minecraft-server
73 stat_fail
77 restart)
78 "$0" stop
79 sleep 1
80 "$0" start
82 status)
83 stat_busy "Checking $_daemonName status"
84 ck_status minecraft-server
87 echo "usage: $0 {start|stop|restart|status}"
88 esac
89 exit 0