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.sh

125 lines
3.3 KiB
Bash
Raw Normal View History

#!/bin/bash
echo
2012-03-01 23:41:37 +01:00
echo "Welcome to: "
echo " ______ __ ______ __ __ _______ .______ ______ .___________."
echo " / || | / __ \ | | | | | \ | _ \ / __ \ | |"
echo "| ,----'| | | | | | | | | | | .--. || |_) | | | | | \`---| |----\`"
echo "| | | | | | | | | | | | | | | || _ < | | | | | | "
echo "| \`----.| \`----.| \`--' | | \`--' | | '--' || |_) | | \`--' | | | "
echo " \______||_______| \______/ \______/ |_______/ |______/ \______/ |__| "
echo " http://git.io/cloudbot by lukeroge "
echo
2012-03-01 23:41:37 +01:00
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
echo "Starting... ($backend)"
start
fi
2012-03-02 00:23:20 +01:00
;;
stop)
if running; then
echo "Stoppinging... ($backend)"
stop
else
echo "Cannot stop! Bot is not already running!"
fi
2012-03-02 00:23:20 +01:00
;;
restart)
if running; then
echo "Restarting... ($backend)"
stop
start
else
echo "Cannot restart! Bot is not already running!"
fi
2012-03-02 00:23:20 +01:00
;;
clear)
clear
;;
status)
status
;;
*)
echo $usage
;;
esac
}
main() {
usage="./cloudbot {start|stop|restart|clear|status}"
2012-03-02 00:23:20 +01:00
locatefiles
checkbackend
setcommands
2012-03-02 00:23:20 +01:00
processargs $1
}
2012-03-02 00:23:20 +01:00
main $*