2022-05-02 12:38:43 +00:00
|
|
|
#!/bin/bash
|
2019-03-24 18:31:54 +00:00
|
|
|
# description: Start or stop telegram-bash-bot
|
|
|
|
#
|
2020-12-03 13:07:39 +00:00
|
|
|
# example service script to run bashbot in background as specified user
|
2020-11-29 14:34:00 +00:00
|
|
|
#
|
2020-12-03 13:07:39 +00:00
|
|
|
# tested on: ubuntu, opensuse, debian
|
2020-11-29 14:34:00 +00:00
|
|
|
#
|
2022-05-03 21:48:49 +00:00
|
|
|
#### $$VERSION$$ v1.52-dev-10-g1ffa890
|
2019-04-01 16:24:05 +00:00
|
|
|
# shellcheck disable=SC2009
|
|
|
|
# shellcheck disable=SC2181
|
2021-03-02 17:26:41 +00:00
|
|
|
# shellcheck disable=SC2250
|
2019-04-01 16:24:05 +00:00
|
|
|
|
2019-03-28 15:51:33 +00:00
|
|
|
#
|
2019-03-24 18:31:54 +00:00
|
|
|
### 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
|
|
|
|
|
2019-04-04 10:45:31 +00:00
|
|
|
# save default values
|
2019-04-02 19:43:38 +00:00
|
|
|
TERM="" # disable bashbot clear and color output
|
2020-06-23 14:35:50 +00:00
|
|
|
runcmd="echo Dry run:" # not activated until you edit lines below
|
2019-03-24 18:31:54 +00:00
|
|
|
|
2019-04-04 10:45:31 +00:00
|
|
|
#######################
|
|
|
|
# Configuration Section
|
2019-03-24 18:31:54 +00:00
|
|
|
|
2019-04-04 10:45:31 +00:00
|
|
|
# edit the next line to fit the user you want to run bashbot, e.g. nobody:
|
2021-03-03 17:44:16 +00:00
|
|
|
runas="nobody"
|
2019-03-24 18:31:54 +00:00
|
|
|
|
2020-11-29 14:34:00 +00:00
|
|
|
# uncomment one of the example lines to fit your system
|
2021-01-05 11:25:49 +00:00
|
|
|
# runcmd="su ${runas} -s /bin/bash -c " # runasuser with *su*
|
2021-03-03 17:44:16 +00:00
|
|
|
# runcmd="/usr/sbin/runuser ${runas} -s /bin/bash -c " # runasuser with *runuser*
|
2019-03-24 18:31:54 +00:00
|
|
|
|
2019-04-04 10:45:31 +00:00
|
|
|
# edit the values of the following lines to fit your config:
|
2021-03-02 17:05:14 +00:00
|
|
|
# your bot name as given to botfather, e.g. mysomething_bot
|
2021-03-03 17:44:16 +00:00
|
|
|
name=""
|
2021-03-18 13:58:18 +00:00
|
|
|
[ -z "${name}" ] && name="unknown"
|
|
|
|
|
2021-01-10 15:01:52 +00:00
|
|
|
# your bot installation dir
|
2021-03-18 13:58:18 +00:00
|
|
|
bashbotdir="/usr/local/telegram-bot-bash"
|
2021-03-02 17:05:14 +00:00
|
|
|
databotdir="${bashbotdir}/data-bot-bash"
|
2021-03-19 13:01:30 +00:00
|
|
|
FIFO="${databotdir}/webhook-fifo-${name}"
|
2021-03-18 13:58:18 +00:00
|
|
|
|
2021-03-02 17:05:14 +00:00
|
|
|
# programs to run
|
2021-03-01 18:40:53 +00:00
|
|
|
bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh"
|
2021-03-18 13:58:18 +00:00
|
|
|
webhook="cd ${bashbotdir}; nohup ${bashbotdir}/bin/process_batch.sh --startbot --watch ${FIFO}"
|
2021-01-10 15:01:52 +00:00
|
|
|
# set additionl parameter, e.g. debug
|
|
|
|
mode=""
|
2019-04-04 10:45:31 +00:00
|
|
|
|
2021-03-18 11:44:17 +00:00
|
|
|
# select logfile for webhook start stop and script errors
|
|
|
|
hooklog="DEBUG"
|
|
|
|
hooklog="WEBHOOK"
|
|
|
|
|
2019-04-04 10:45:31 +00:00
|
|
|
# END Configuration
|
|
|
|
#######################
|
|
|
|
|
2021-03-18 11:35:14 +00:00
|
|
|
|
|
|
|
# check for bot status
|
2022-05-02 12:38:43 +00:00
|
|
|
stat=""
|
2021-03-18 11:35:14 +00:00
|
|
|
ps -f -u "${runas}" | grep "${name}" | grep -qF "bashbot.sh startbot"
|
|
|
|
if [ "$?" = "0" ]; then
|
|
|
|
# printf "bashbot (%s) is running in poll mode\n" "${name}"
|
2022-05-02 12:38:43 +00:00
|
|
|
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"
|
2021-03-18 11:35:14 +00:00
|
|
|
else
|
2022-05-02 12:38:43 +00:00
|
|
|
stat="unknown"
|
2021-03-18 11:35:14 +00:00
|
|
|
fi
|
2019-03-24 18:31:54 +00:00
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
'start')
|
2022-05-02 12:38:43 +00:00
|
|
|
[ "${stat}" != "stop" ] && printf "Warning, bot is already running in mode: %s\n" "${stat}"
|
2021-01-10 15:01:52 +00:00
|
|
|
$runcmd "$bashbot start $mode" # >/dev/null 2>&1 </dev/null
|
2019-03-24 18:31:54 +00:00
|
|
|
RETVAL=$?
|
|
|
|
;;
|
2021-03-02 17:05:14 +00:00
|
|
|
'starthook')
|
2021-03-18 13:09:42 +00:00
|
|
|
[ -p "${FIFO}" ] || printf "Warning, webhook pipe not found: %s\n" "${FIFO##*/}"
|
2022-05-02 12:38:43 +00:00
|
|
|
[ "${stat}" != "stop" ] && printf "Warning, bot is already running in mode: %s\n" "${stat}"
|
2021-03-10 07:39:17 +00:00
|
|
|
printf "Starting bashbot in webhook mode ... "
|
2021-03-18 11:44:17 +00:00
|
|
|
$runcmd "$webhook $mode </dev/null &>>${bashbotdir}/logs/${hooklog}.log &" # >/dev/null 2>&1 </dev/null
|
2021-03-02 17:26:41 +00:00
|
|
|
sleep 1
|
|
|
|
$0 status
|
2021-03-02 17:05:14 +00:00
|
|
|
RETVAL=$?
|
|
|
|
;;
|
2019-03-24 18:31:54 +00:00
|
|
|
'stop')
|
2022-05-03 21:48:49 +00:00
|
|
|
[[ "${stat}" != *"poll"* ]] && printf "Warning, bot is not in poll mode: %s\n" "${stat}"
|
2021-01-10 15:01:52 +00:00
|
|
|
$runcmd "$bashbot stop $mode"
|
2019-03-24 18:31:54 +00:00
|
|
|
RETVAL=$?
|
|
|
|
;;
|
2021-03-02 17:05:14 +00:00
|
|
|
'stophook')
|
2022-05-03 21:48:49 +00:00
|
|
|
[[ "${stat}" != *"hook"* ]] && printf "Warning, bot is not in webhook mode: %s\n" "${stat}"
|
2021-03-10 07:39:17 +00:00
|
|
|
printf "Stopping bashbot webhook mode ... "
|
2021-06-03 10:03:10 +00:00
|
|
|
KILLID="$(ps -f -u "${runas}" | grep "process_batch.sh --startbot" | sed -E 's/[^0-9]+([0-9]+).*/\1/' | tr -s "\r\n" " ")"
|
2021-03-02 17:26:41 +00:00
|
|
|
if [ -n "${KILLID}" ]; then
|
2022-05-02 12:38:43 +00:00
|
|
|
$runcmd "kill ${KILLID} 2>/dev/null; wait ${KILLID} 2>/dev/null"
|
2021-03-10 07:39:17 +00:00
|
|
|
sleep 1
|
2021-03-02 17:26:41 +00:00
|
|
|
fi
|
2021-03-02 17:05:14 +00:00
|
|
|
RETVAL=$?
|
2021-03-18 13:09:42 +00:00
|
|
|
$0 status
|
2021-03-02 17:05:14 +00:00
|
|
|
;;
|
2019-03-24 18:31:54 +00:00
|
|
|
'status')
|
2021-03-18 11:35:14 +00:00
|
|
|
case "${stat}" in
|
2022-05-02 12:38:43 +00:00
|
|
|
*"poll"*) printf "bashbot (%s) is running in polling mode\n" "${name}"
|
2021-03-02 17:05:14 +00:00
|
|
|
RETVAL=0
|
2022-05-02 12:38:43 +00:00
|
|
|
;;&
|
|
|
|
*"hook"*) printf "bashbot (%s) is running in webhook mode\n" "${name}"
|
2021-03-18 11:35:14 +00:00
|
|
|
RETVAL=0
|
|
|
|
;;
|
2022-05-02 12:38:43 +00:00
|
|
|
*"stop"*) printf "bashbot (%s) is not running\n" "${name}"
|
2021-03-02 17:05:14 +00:00
|
|
|
RETVAL=1
|
2021-03-18 11:35:14 +00:00
|
|
|
;;
|
|
|
|
*) printf "bashbot (%s) status is %s\n" "${name}" "${stat}"
|
|
|
|
RETVAL=2
|
|
|
|
;;
|
|
|
|
esac
|
2019-03-24 18:31:54 +00:00
|
|
|
;;
|
|
|
|
'restart'|'reload')
|
2019-03-25 10:11:22 +00:00
|
|
|
$0 stop; $0 start
|
2019-03-24 18:31:54 +00:00
|
|
|
RETVAL=$?
|
|
|
|
;;
|
2021-03-02 17:05:14 +00:00
|
|
|
'restarthook'|'reloadhook')
|
|
|
|
$0 stophook; $0 starthook
|
|
|
|
RETVAL=$?
|
|
|
|
;;
|
2019-04-27 13:48:03 +00:00
|
|
|
'restartback')
|
|
|
|
$0 suspendback; $0 resumeback
|
|
|
|
RETVAL=$?
|
|
|
|
;;
|
2019-03-24 18:31:54 +00:00
|
|
|
'suspendback'|'resumeback'|'killback')
|
2021-01-05 11:25:49 +00:00
|
|
|
# shellcheck disable=SC2250
|
2022-05-02 12:38:43 +00:00
|
|
|
$runcmd "$bashbot $1"
|
2019-03-25 10:11:22 +00:00
|
|
|
RETVAL=$?
|
2020-06-07 11:30:59 +00:00
|
|
|
# kill inotifywait from runuser
|
|
|
|
if [ "$1" != "resumeback" ]; then
|
|
|
|
# shellcheck disable=SC2046
|
2021-01-05 11:25:49 +00:00
|
|
|
kill -9 $(ps -u "${runas}" | grep inotifywait | sed 's/ .*//') >/dev/null 2>&1
|
2020-05-14 11:04:57 +00:00
|
|
|
fi
|
2019-03-24 18:31:54 +00:00
|
|
|
;;
|
|
|
|
*)
|
2021-03-03 07:55:49 +00:00
|
|
|
printf "%s\n" "Usage: $0 [ start | stop | restart | starthook | stophook | restarthook ]"
|
|
|
|
printf "%s\n" " $0 [ status | restartback | suspendback | resumeback | killback ]"
|
2019-03-24 18:31:54 +00:00
|
|
|
RETVAL=1
|
|
|
|
;;
|
|
|
|
esac
|
2021-01-05 11:25:49 +00:00
|
|
|
exit "${RETVAL}"
|