-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbtctl
115 lines (105 loc) · 2.2 KB
/
btctl
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
#!/bin/bash
LABEL='bt'
USERNAME='battletech'
GAMEPATH='/srv/bttt'
SERVICE='bttt'
PARAMS='-dedicated'
FIRST_CMD=''
ME=`whoami`
as_user()
{
if [ $ME == $USERNAME ]
then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
bt_start()
{
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
as_user "screen -wipe >/dev/null"
echo "Starting $SERVICE..."
as_user "cd $GAMEPATH && screen -S $LABEL -dm /bin/sh -c 'stdbuf -oL -eL ./$SERVICE $PARAMS >>server.log 2>>server.log'"
sleep 0.5
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is now running."
if [ ! -z "$FIRST_CMD" ]
then
bt_command "$FIRST_CMD"
fi
else
echo "Error! Could not start $SERVICE!"
fi
fi
}
bt_stop()
{
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE..."
as_user "screen -p 0 -S $LABEL -X eval 'stuff \"quit\"\015'"
sleep 0.5
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
killall -KILL $SERVICE
echo "$SERVICE killed."
else
echo "$SERVICE is stopped."
fi
}
bt_command()
{
command="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
pre_log_len=`wc -l "$GAMEPATH/server.log" | awk '{print $1}'`
echo "$SERVICE executing command: $command"
as_user "screen -p 0 -S $LABEL -X eval 'stuff \"${command//;/\\015}\"\015'"
sleep 0.1 # assumes that the command will run and print to the log file in less than .1 seconds
# print output
tail -n $[`wc -l "$GAMEPATH/server.log" | awk '{print $1}'`-$pre_log_len] "$GAMEPATH/server.log"
fi
}
#Start-Stop here
case "$1" in
start)
bt_start
;;
stop)
bt_stop
;;
restart)
bt_stop
bt_start
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
command)
if [ $# -gt 1 ]
then
shift
bt_command "$*"
else
echo "Must specify server command."
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status|command \"server command\"}"
exit 1
;;
esac
exit 0