This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/cloudbot

125 lines
3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
2012-03-02 03:03:41 +01:00
echo ""
2012-03-02 02:46:17 +01:00
echo " ________ ______ __ "
echo " / ____/ /___ __ ______/ / __ )____ / /_"
echo " / / / / __ \/ / / / __ / __ / __ \/ __/"
echo "/ /___/ / /_/ / /_/ / /_/ / /_/ / /_/ / /_ "
echo "\____/_/\____/\__,_/\__,_/_____/\____/\__/ "
2012-03-02 03:03:41 +01:00
echo " http://git.io/cloudbot by lukeroge "
2012-03-02 02:46:17 +01:00
echo ""
2012-03-02 00:23:20 +01:00
locatefiles() {
botfile="/bot.py"
botfile=$(pwd)$botfile
logfile="/bot.log"
2012-03-02 00:23:20 +01:00
logfile=$(pwd)$logfile
}
running() {
if [[ $(ps aux|grep bot.py|grep -v grep|grep -v daemon|grep -v screen) != "" ]]; then
true
else
false
fi
}
checkbackend() {
if dpkg -l| grep ^ii|grep daemon|grep 'turns other' > /dev/null; then
2012-03-01 23:41:37 +01:00
backend="daemon"
elif dpkg -l| grep ^ii|grep screen|grep 'terminal multi' > /dev/null; then
2012-03-01 23:41:37 +01:00
backend="screen"
else
2012-03-01 23:41:37 +01:00
backend="manual"
fi
return 0
}
setcommands() {
status() {
if running; then
echo "CloudBot is running!"
else
echo "CloudBot is not running!"
fi
}
clear() {
: > $logfile
}
2012-03-01 23:41:37 +01:00
if [ "$backend" == "daemon" ]; then
2012-03-02 00:23:20 +01:00
start() {
daemon -r -n cloudbot -O $logfile python $botfile
}
stop() {
daemon -n cloudbot --stop
}
2012-03-01 23:41:37 +01:00
elif [ "$backend" == "screen" ]; then
2012-03-02 00:23:20 +01:00
start() {
screen -d -m -S cloudbot -t cloudbot python $botfile > $logfile 2>&1
}
stop() {
proc=`ps ax|grep -v grep|grep screen|grep $botfile`
pid=`top -n 1 -p ${proc:0:5} | grep ${proc:0:5}`
kill $pid
2012-03-02 00:23:20 +01:00
}
2012-03-01 23:41:37 +01:00
elif [ "$backend" == "manual" ]; then
2012-03-02 00:23:20 +01:00
start() {
$botfile
}
stop() {
proc=`ps ax|grep -v grep|grep python|grep $botfile`
pid=`top -n 1 -p ${proc:0:5} | grep ${proc:0:5}`
kill $pid
2012-03-02 00:23:20 +01:00
}
fi
}
processargs() {
2012-03-02 00:23:20 +01:00
case $1 in
start)
if running; then
echo "Cannot start! Bot is already running!"
else
2012-03-02 02:46:17 +01:00
echo "Starting CloudBot... ($backend)"
start
fi
2012-03-02 00:23:20 +01:00
;;
stop)
if running; then
2012-03-02 02:46:17 +01:00
echo "Stopping CloudBot... ($backend)"
stop
else
echo "Cannot stop! Bot is not already running!"
fi
2012-03-02 00:23:20 +01:00
;;
restart)
if running; then
2012-03-02 02:46:17 +01:00
echo "Restarting CloudBot... ($backend)"
stop
start
else
echo "Cannot restart! Bot is not already running!"
fi
2012-03-02 00:23:20 +01:00
;;
clear)
echo "Clearing logs..."
2012-03-02 00:23:20 +01:00
clear
;;
status)
status
;;
2012-03-02 02:46:17 +01:00
*)
echo "Please enter a command:"
2012-03-02 02:03:33 +01:00
usage="./cloudbot {start|stop|restart|clear|status}"
2012-03-02 00:23:20 +01:00
echo $usage
;;
esac
}
main() {
2012-03-02 00:23:20 +01:00
locatefiles
checkbackend
setcommands
2012-03-02 00:23:20 +01:00
processargs $1
}
2012-03-02 02:46:17 +01:00
main $*