mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-14 04:24:02 +00:00
155 lines
4.3 KiB
Bash
Executable File
155 lines
4.3 KiB
Bash
Executable File
#!/bin/bash
|
|
# description: Start or stop telegram-bash-bot
|
|
#
|
|
# example service script to run bashbot in background as specified user
|
|
#
|
|
# tested on: ubuntu, opensuse, debian
|
|
#
|
|
#### $$VERSION$$ v1.52-dev-10-g1ffa890
|
|
# shellcheck disable=SC2009
|
|
# shellcheck disable=SC2181
|
|
# shellcheck disable=SC2250
|
|
|
|
#
|
|
### BEGIN INIT INFO
|
|
# Provides: bashbot
|
|
# Required-Start: $network $syslog
|
|
# Required-Stop: $network
|
|
# Default-Start: 2 3 5
|
|
# Default-Stop: 0 1 6
|
|
# Description: Start or stop telegram-bot-bash server
|
|
### END INIT INFO
|
|
|
|
# save default values
|
|
TERM="" # disable bashbot clear and color output
|
|
runcmd="echo Dry run:" # not activated until you edit lines below
|
|
|
|
#######################
|
|
# Configuration Section
|
|
|
|
# edit the next line to fit the user you want to run bashbot, e.g. nobody:
|
|
runas="nobody"
|
|
|
|
# uncomment one of the example lines to fit your system
|
|
# runcmd="su ${runas} -s /bin/bash -c " # runasuser with *su*
|
|
# runcmd="/usr/sbin/runuser ${runas} -s /bin/bash -c " # runasuser with *runuser*
|
|
|
|
# edit the values of the following lines to fit your config:
|
|
# your bot name as given to botfather, e.g. mysomething_bot
|
|
name=""
|
|
[ -z "${name}" ] && name="unknown"
|
|
|
|
# your bot installation dir
|
|
bashbotdir="/usr/local/telegram-bot-bash"
|
|
databotdir="${bashbotdir}/data-bot-bash"
|
|
FIFO="${databotdir}/webhook-fifo-${name}"
|
|
|
|
# programs to run
|
|
bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh"
|
|
webhook="cd ${bashbotdir}; nohup ${bashbotdir}/bin/process_batch.sh --startbot --watch ${FIFO}"
|
|
# set additionl parameter, e.g. debug
|
|
mode=""
|
|
|
|
# select logfile for webhook start stop and script errors
|
|
hooklog="DEBUG"
|
|
hooklog="WEBHOOK"
|
|
|
|
# END Configuration
|
|
#######################
|
|
|
|
|
|
# check for bot status
|
|
stat=""
|
|
ps -f -u "${runas}" | grep "${name}" | grep -qF "bashbot.sh startbot"
|
|
if [ "$?" = "0" ]; then
|
|
# printf "bashbot (%s) is running in poll mode\n" "${name}"
|
|
stat="${stat} polling"
|
|
fi
|
|
ps -f -u "${runas}" | grep "${name}" | grep -qF "process_batch.sh --startbot"
|
|
if [ "$?" = "0" ]; then
|
|
#printf "bashbot (%s) is running in webhook mode\n" "${name}"
|
|
stat="${stat} webhook"
|
|
elif [ "${name}" != "unknown" ]; then
|
|
#printf "bashbot (%s) is stopped\n" "${name}"
|
|
stat="stop"
|
|
else
|
|
stat="unknown"
|
|
fi
|
|
|
|
case "$1" in
|
|
'start')
|
|
[ "${stat}" != "stop" ] && printf "Warning, bot is already running in mode: %s\n" "${stat}"
|
|
$runcmd "$bashbot start $mode" # >/dev/null 2>&1 </dev/null
|
|
RETVAL=$?
|
|
;;
|
|
'starthook')
|
|
[ -p "${FIFO}" ] || printf "Warning, webhook pipe not found: %s\n" "${FIFO##*/}"
|
|
[ "${stat}" != "stop" ] && printf "Warning, bot is already running in mode: %s\n" "${stat}"
|
|
printf "Starting bashbot in webhook mode ... "
|
|
$runcmd "$webhook $mode </dev/null &>>${bashbotdir}/logs/${hooklog}.log &" # >/dev/null 2>&1 </dev/null
|
|
sleep 1
|
|
$0 status
|
|
RETVAL=$?
|
|
;;
|
|
'stop')
|
|
[[ "${stat}" != *"poll"* ]] && printf "Warning, bot is not in poll mode: %s\n" "${stat}"
|
|
$runcmd "$bashbot stop $mode"
|
|
RETVAL=$?
|
|
;;
|
|
'stophook')
|
|
[[ "${stat}" != *"hook"* ]] && printf "Warning, bot is not in webhook mode: %s\n" "${stat}"
|
|
printf "Stopping bashbot webhook mode ... "
|
|
KILLID="$(ps -f -u "${runas}" | grep "process_batch.sh --startbot" | sed -E 's/[^0-9]+([0-9]+).*/\1/' | tr -s "\r\n" " ")"
|
|
if [ -n "${KILLID}" ]; then
|
|
$runcmd "kill ${KILLID} 2>/dev/null; wait ${KILLID} 2>/dev/null"
|
|
sleep 1
|
|
fi
|
|
RETVAL=$?
|
|
$0 status
|
|
;;
|
|
'status')
|
|
case "${stat}" in
|
|
*"poll"*) printf "bashbot (%s) is running in polling mode\n" "${name}"
|
|
RETVAL=0
|
|
;;&
|
|
*"hook"*) printf "bashbot (%s) is running in webhook mode\n" "${name}"
|
|
RETVAL=0
|
|
;;
|
|
*"stop"*) printf "bashbot (%s) is not running\n" "${name}"
|
|
RETVAL=1
|
|
;;
|
|
*) printf "bashbot (%s) status is %s\n" "${name}" "${stat}"
|
|
RETVAL=2
|
|
;;
|
|
esac
|
|
;;
|
|
'restart'|'reload')
|
|
$0 stop; $0 start
|
|
RETVAL=$?
|
|
;;
|
|
'restarthook'|'reloadhook')
|
|
$0 stophook; $0 starthook
|
|
RETVAL=$?
|
|
;;
|
|
'restartback')
|
|
$0 suspendback; $0 resumeback
|
|
RETVAL=$?
|
|
;;
|
|
'suspendback'|'resumeback'|'killback')
|
|
# shellcheck disable=SC2250
|
|
$runcmd "$bashbot $1"
|
|
RETVAL=$?
|
|
# kill inotifywait from runuser
|
|
if [ "$1" != "resumeback" ]; then
|
|
# shellcheck disable=SC2046
|
|
kill -9 $(ps -u "${runas}" | grep inotifywait | sed 's/ .*//') >/dev/null 2>&1
|
|
fi
|
|
;;
|
|
*)
|
|
printf "%s\n" "Usage: $0 [ start | stop | restart | starthook | stophook | restarthook ]"
|
|
printf "%s\n" " $0 [ status | restartback | suspendback | resumeback | killback ]"
|
|
RETVAL=1
|
|
;;
|
|
esac
|
|
exit "${RETVAL}"
|