-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.sh
182 lines (170 loc) · 3.38 KB
/
server.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# !/bin/bash
#
# Load config
#SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
SCRIPT_DIR="$(dirname "$0")"
SERVER_SETTINGS="$SCRIPT_DIR/server.cfg"
. $SERVER_SETTINGS
# Run as designated user
ME=`whoami`
as_user() {
if [ $ME == $SERVER_USER ]
then
bash -c "$1"
else
sudo -u $SERVER_USER bash -c "$1"
fi
}
# Check if server is running
is_running() {
if pgrep -u $SERVER_USER -f $SERVER_JAR > /dev/null
then
return 0
else
return 1
fi
}
server_start() {
if is_running
then
echo "$SERVER_NAME is already running."
else
echo "Starting $SERVER_NAME..."
PRE_LOG_LENGTH=`wc -l "$LOG_FILE" | awk '{print $1}'`
INVOCATION="$SERVER_EXE $SERVER_PARAMS"
as_user "cd $SERVER_DIR && screen -dmS $SCREEN_NAME $INVOCATION"
sleep $START_TIME
if is_running
then
echo "$SERVER_NAME is now running."
else
echo "Could not start $SERVER_NAME."
fi
# Print output
CURRENT_LOG_LENGTH=`wc -l "$LOG_FILE" | awk '{print $1}'`
tail -n $((CURRENT_LOG_LENGTH-PRE_LOG_LENGTH)) "$LOG_FILE"
fi
}
server_stop() {
if is_running
then
echo "$SERVER_NAME is running. Stopping server..."
server_command $STOP_COMMAND
sleep $STOP_TIME
if is_running
then
echo "$SERVER_NAME is still running."
else
echo "$SERVER_NAME has stopped."
fi
else
echo "$SERVER_NAME is not running."
fi
}
server_status() {
if is_running
then
echo "$SERVER_NAME is running."
else
echo "$SERVER_NAME is not running."
fi
}
server_command() {
COMMAND=$*
if [ -z "$COMMAND" ]
then
# Show full command usage when for no args when sending server commands by default
#echo "Usage: $0 { start | stop | restart | status | save | screen | backup | cmd <command> }"
echo "Usage: $0 cmd <command>"
return
fi
if is_running
then
echo "$SERVER_NAME is running. Executing $COMMAND..."
PRE_LOG_LENGTH=`wc -l "$LOG_FILE" | awk '{print $1}'`
as_user "screen -p 0 -S $SCREEN_NAME -X eval 'stuff \"$COMMAND\"\015'"
sleep $COMMAND_TIME
# Print output
CURRENT_LOG_LENGTH=`wc -l "$LOG_FILE" | awk '{print $1}'`
tail -n $((CURRENT_LOG_LENGTH-PRE_LOG_LENGTH)) "$LOG_FILE"
else
echo "$SERVER_NAME is not running."
fi
}
server_save() {
for CMD in ${!SAVE_COMMANDS[*]}
do
server_command ${SAVE_COMMANDS[$CMD]}
done
}
server_screen() {
if is_running
then
as_user "screen -p 0 -S $SCREEN_NAME -r"
else
echo "$SERVER_NAME is not running."
fi
}
server_backup() {
# Save server
server_save
# Disable saving
for CMD in ${!SAVE_DISABLE_COMMANDS[*]}
do
server_command ${SAVE_DISABLE_COMMANDS[$CMD]}
done
# Archive files
for FILE in ${!BACKUP[*]}
do
ARGS=${BACKUP[$FILE]}
echo "Backing up $FILE..."
as_user "cd $SERVER_DIR && 7za a $BACKUP_DIR/${FILE}_${BACKUP_TIME}.zip $ARGS"
echo "$FILE has been archived."
done
# Re-enable saving
for CMD in ${!SAVE_ENABLE_COMMANDS[*]}
do
server_command ${SAVE_ENABLE_COMMANDS[$CMD]}
done
echo "Server backup complete."
}
case $1 in
start)
server_start
;;
stop)
server_stop
;;
restart)
server_stop
server_start
;;
status)
server_status
;;
save)
server_save
;;
screen)
server_screen
;;
cmd)
server_command ${@:2}
;;
command)
server_command ${@:2}
;;
spigot-restart)
sleep $SPIGOT_RESTART_TIME
server_start
;;
backup)
server_backup
;;
*)
# Optional: pass command to server by default
#server_command $*
echo "Usage: $0 { start | stop | restart | status | save | screen | backup | cmd <command> }"
;;
esac
exit 0