Skip to content

Commit d7432fe

Browse files
committed
inital commit
1 parent 272ec27 commit d7432fe

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

sinusbot

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#! /bin/bash
2+
### BEGIN INIT INFO
3+
# Provides: sinusbot
4+
# Required-Start: $local_fs $network
5+
# Required-Stop: $local_fs $network
6+
# Default-Start: 2 3 4 5
7+
# Default-Stop: 0 1 6
8+
# Description: Sinusbot
9+
### END INIT INFO
10+
11+
##################################################################################
12+
# #
13+
# Usage: ./launch.sh {start|stop|status|restart|console|update|backup} #
14+
# - start: start the bot #
15+
# - stop: stop the bot #
16+
# - status: display the status of the bot (down or up) #
17+
# - restart: restart the bot #
18+
# - console: display the bot console #
19+
# - update: runs the bot updater (with start & stop)
20+
# - backup: archives your bot root directory
21+
# To exit the console without stopping the server, press CTRL + A then D. #
22+
# #
23+
##################################################################################
24+
25+
SCREEN_NAME="sinusbot"
26+
USER="mybotuser"
27+
DIR_ROOT="/opt/ts3soundboard/"
28+
DIR_BACKUP=""
29+
BOT_RUNCMD="./sinusbot"
30+
BOT_UPDATE_ARG=" -update"
31+
SCREEN_IGNORE_TTY="N" # Y = Ignore the tty, you can access the bot screen as root (i not recommend it) root should be disabled :) | N = disabled
32+
33+
# No edits necessary beyond this line
34+
PATH=/bin:/usr/bin:/sbin:/usr/sbin
35+
if [ ! -x `which screen` ]; then echo "ERROR: You need screen for this script (try apt-get install screen)"; exit 1; fi
36+
if [ ! -x `which tar` ]; then echo "WARNING: You need tar for the Backup Function (try apt-get install tar)"; fi
37+
38+
function start {
39+
if [ ! -d $DIR_ROOT ]; then echo "ERROR: $DIR_ROOT is not a directory"; exit 1; fi
40+
if status; then echo "$SCREEN_NAME is already running"; exit 1; fi
41+
42+
# Start bot
43+
if [ `whoami` = root ]
44+
then
45+
su - $USER -c "cd $DIR_ROOT ; screen -AmdS $SCREEN_NAME $BOT_RUNCMD"
46+
else
47+
cd $DIR_ROOT
48+
screen -AmdS $SCREEN_NAME $BOT_RUNCMD
49+
fi
50+
}
51+
52+
function backup {
53+
54+
DATE=$(date +%Y-%m-%d)
55+
56+
if [ `whoami` = root ]
57+
then
58+
su - $USER -c "cd $DIR_ROOT ; tar -cjpf $DIR_BACKUP/sinusbot-$DATE.tar.bz2 $DIR_ROOT"
59+
else
60+
cd $DIR_ROOT
61+
tar -cjpf $DIR_BACKUP/sinusbot-$DATE.tar.bz2 $DIR_ROOT
62+
fi
63+
}
64+
function update {
65+
if [ `whoami` = root ]
66+
then
67+
su - $USER -c "cd $DIR_ROOT ; $BOT_RUNCMD$BOT_UPDATE_ARG"
68+
else
69+
cd $DIR_ROOT
70+
$BOT_RUNCMD$BOT_UPDATE_ARG
71+
fi
72+
}
73+
74+
function stop {
75+
if ! status; then echo "$SCREEN_NAME could not be found. Probably not running."; exit 1; fi
76+
77+
if [ `whoami` = root ]
78+
then
79+
su - $USER -c "screen -S $SCREEN_NAME -X stuff '\003'"
80+
else
81+
screen -S $SCREEN_NAME -X stuff '\003'
82+
fi
83+
}
84+
85+
function status {
86+
if [ `whoami` = root ]
87+
then
88+
su - $USER -c "screen -ls" | grep [.]$SCREEN_NAME[[:space:]] > /dev/null
89+
else
90+
screen -ls | grep [.]$SCREEN_NAME[[:space:]] > /dev/null
91+
fi
92+
}
93+
94+
function console {
95+
if ! status; then echo "$SCREEN_NAME could not be found. Probably not running."; exit 1; fi
96+
97+
if [ `whoami` = root ]
98+
then
99+
100+
if [ $SCREEN_IGNORE_TTY == 'Y' ]
101+
then
102+
su - $USER -c "script -q -c 'screen -x $SCREEN_NAME' /dev/null"
103+
else
104+
su - $USER -c "screen -x $SCREEN_NAME"
105+
fi
106+
else
107+
screen -x $SCREEN_NAME
108+
fi
109+
}
110+
111+
function usage {
112+
echo "Usage: $0 {start|stop|status|restart|console}"
113+
echo "On console, press CTRL+A then D to stop the screen without stopping the server."
114+
}
115+
116+
case "$1" in
117+
118+
start)
119+
echo "Using following data:"
120+
echo "USER: $USER"
121+
echo "DIR ROOT: $DIR_ROOT"
122+
echo "BOT RUN CMD: $BOT_RUNCMD"
123+
echo ""
124+
sleep 2
125+
echo "Starting $SCREEN_NAME..."
126+
start
127+
sleep 2
128+
echo "$SCREEN_NAME started successfully"
129+
;;
130+
131+
stop)
132+
echo "Stopping $SCREEN_NAME..."
133+
stop
134+
sleep 2
135+
echo "$SCREEN_NAME stopped successfully"
136+
;;
137+
138+
restart)
139+
echo "Restarting $SCREEN_NAME..."
140+
status && stop
141+
sleep 5
142+
start
143+
sleep 2
144+
echo "$SCREEN_NAME restarted successfully"
145+
;;
146+
147+
status)
148+
if status
149+
then echo "$SCREEN_NAME is UP"
150+
else echo "$SCREEN_NAME is DOWN"
151+
fi
152+
;;
153+
154+
console)
155+
echo "Open console on $SCREEN_NAME..."
156+
console
157+
;;
158+
159+
update)
160+
if status
161+
then stop && sleep 5 && update
162+
else update
163+
fi
164+
165+
;;
166+
167+
backup)
168+
169+
if [ -d "$DIR_BACKUP" ]
170+
then
171+
if status
172+
then stop && sleep 5 && backup
173+
else backup
174+
fi
175+
else echo "BACKUP DIRECTORY NOT EXISTS. EXIT!"; exit 1;
176+
fi
177+
178+
;;
179+
180+
*)
181+
usage
182+
exit 1
183+
;;
184+
185+
esac
186+
187+
exit 0
188+

0 commit comments

Comments
 (0)