blob: 273b2f2162b8df2c43564c7abb28f785cc7bdded (
plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#!/bin/sh
#
# $FreeBSD$
# PROVIDE: minecraft
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# rc.conf variables:
# minecraft_enable (Default: NO)
# minecraft_mems (Default: 1024M)
# minecraft_memx (Default: 1024M)
# minecraft_args (Default: "")
. /etc/rc.subr
name=minecraft
rcvar=minecraft_enable
desc="Dedicated server for the game Minecraft"
load_rc_config $name
minecraft_mems=${minecraft_mems:-"1024M"}
minecraft_memx=${minecraft_memx:-"1024M"}
minecraft_args=${minecraft_args:-""}
extra_commands="console status kill"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
console_cmd="${name}_console"
status_cmd="${name}_status"
kill_cmd="${name}_kill"
is_running()
{
pgrep -qu mcserver java
return $?
}
minecraft_start()
{
if is_running; then
echo "${name} is already running."
elif [ ${minecraft_mems%?} -gt ${minecraft_memx%?} ]; then
echo "ERROR: minecraft_mems is set greater than minecraft_memx."
echo "ERROR: Set minecraft_memx to be equal or greater than minecraft_mems in rc.conf."
else
if [ -n "${minecraft_mem}" ]; then
echo "WARNING: minecraft_mem is deprecated. Please change this to minecraft_memx."
minecraft_memx=${minecraft_mem}
fi
echo "Starting ${name}."
cd %%PREFIX%%/minecraft-server
su mcserver -c "%%LOCALBASE%%/bin/tmux new-session -s minecraft -d '%%LOCALBASE%%/bin/java -Xmx${minecraft_memx} -Xms${minecraft_mems} ${minecraft_args} -jar %%PREFIX%%/minecraft-server/server.jar nogui'"
fi
}
minecraft_stop()
{
if is_running; then
echo "Stopping ${name}."
su mcserver -c "%%LOCALBASE%%/bin/tmux send-keys -t ${name} \"stop\" ENTER"
i=0
while [ $i -lt 10 ]; do
i=$(($i + 1))
if is_running; then
sleep 1
else
echo "${name} has been stopped."
return
fi
done
echo "WARN: ${name} could not be stopped or is taking longer than expected."
echo "WARN: To view the console, type 'service ${name} console'"
echo "WARN: To kill ${name}, type 'service ${name} kill'"
else
echo "${name} is not running."
fi
}
minecraft_console()
{
if ! is_running; then
echo "${name} is not running."
else
export TERM=xterm
su mcserver -c "%%LOCALBASE%%/bin/tmux attach-session -t ${name}"
fi
}
minecraft_status()
{
if is_running; then
echo "${name} is running."
else
echo "${name} is not running."
fi
}
minecraft_kill()
{
if ! is_running; then
echo "${name} is not running."
else
pkill -KILL -u mcserver java
echo "${name} has been killed."
fi
}
run_rc_command "$1"
|