diff --git a/cloudbot.sh b/cloudbot.sh index fd7a14b..1a5d16a 100755 --- a/cloudbot.sh +++ b/cloudbot.sh @@ -10,71 +10,36 @@ echo " \______||_______| \______/ \______/ |_______/ |______/ \______/ echo " http://git.io/cloudbot by lukeroge " echo -args=$* -usage="./cloudbot {start|stop|restart|clear|status}" - locatefiles() { botfile="/bot.py" botfile=$(pwd)$botfile - logfile="/botlog" + logfile="/bot.log" 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 backend="daemon" - echo "backend: daemon" elif dpkg -l| grep ^ii|grep screen|grep 'terminal multi' > /dev/null; then backend="screen" - echo "backend: screen" else backend="manual" - echo "backend: manual" fi return 0 } -running() { - ps ax|grep bot|grep -v grep|grep -v ./cloudbot - return $? -} - setcommands() { - if [ "$backend" == "daemon" ]; then - start() { - daemon -r -n cloudbot -O $logfile python $botfile - } - stop() { - daemon -n cloudbot --stop - } - pid() { - pidof /usr/bin/daemon - } - elif [ "$backend" == "screen" ]; then - start() { - screen -d -m -S cloudbot -t cloudbot python $botfile > $logfile 2>&1 - } - stop() { - kill $(pidof /usr/bin/screen) - } - pid() { - pidof /usr/bin/screen - } - elif [ "$backend" == "manual" ]; then - start() { - $botfile - } - stop() { - kill $(pidof $botfile) - } - pid() { - pidof $botfile - } - fi status() { if running; then echo "CloudBot is running!" - pid else echo "CloudBot is not running!" fi @@ -82,19 +47,60 @@ setcommands() { clear() { : > $logfile } + if [ "$backend" == "daemon" ]; then + start() { + daemon -r -n cloudbot -O $logfile python $botfile + } + stop() { + daemon -n cloudbot --stop + } + elif [ "$backend" == "screen" ]; then + 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 + } + elif [ "$backend" == "manual" ]; then + 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 + } + fi } processargs() { case $1 in start) - start + if running; then + echo "Cannot start! Bot is already running!" + else + echo "Starting... ($backend)" + start + fi ;; stop) - stop + if running; then + echo "Stoppinging... ($backend)" + stop + else + echo "Cannot stop! Bot is not already running!" + fi ;; restart) - stop - start + if running; then + echo "Restarting... ($backend)" + stop + start + else + echo "Cannot restart! Bot is not already running!" + fi ;; clear) clear @@ -109,6 +115,7 @@ processargs() { } main() { + usage="./cloudbot {start|stop|restart|clear|status}" locatefiles checkbackend setcommands