telegram-bot-bash/bashbot.rc

118 lines
3.0 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# description: Start or stop telegram-bash-bot
#
# example service script to run bashbot in background as specified user
2020-11-29 14:34:00 +00:00
#
# tested on: ubuntu, opensuse, debian
2020-11-29 14:34:00 +00:00
#
#### $$VERSION$$ v1.45-dev-60-g2415814
2019-04-01 16:24:05 +00:00
# shellcheck disable=SC2009
# shellcheck disable=SC2181
#
### 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
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-04-04 10:45:31 +00:00
#######################
# Configuration Section
2019-04-04 10:45:31 +00:00
# edit the next line to fit the user you want to run bashbot, e.g. nobody:
2020-12-03 13:58:32 +00:00
runas="nobody"
2020-11-29 14:34:00 +00:00
# uncomment one of the example lines to fit your system
# runcmd="su ${runas} -s /bin/bash -c " # runasuser with *su*
# runcmd="runuser ${runas} -s /bin/bash -c " # runasuser with *runuser*
2019-04-04 10:45:31 +00:00
# edit the values of the following lines to fit your config:
# your bot name as given to botfather, e.g. mysomething_bot
name=""
2021-01-10 15:01:52 +00:00
# your bot installation dir
2021-03-01 18:40:53 +00:00
bashbotdir="/usr/local/telegram-bot-bash"
databotdir="${bashbotdir}/data-bot-bash"
# programs to run
2021-03-01 18:40:53 +00:00
bashbot="cd ${bashbotdir}; ${bashbotdir}/bashbot.sh"
webhook="cd ${bashbotdir}; ${bashbotdir}/bin/process_batch.sh --start --watch ${databotdir}/webhook-fifo-${name}"
2021-01-10 15:01:52 +00:00
# set additionl parameter, e.g. debug
mode=""
2019-04-04 10:45:31 +00:00
# END Configuration
#######################
[ "${name}" = "" ] && name="${runas}"
case "$1" in
'start')
# shellcheck disable=SC2250
2021-01-10 15:01:52 +00:00
$runcmd "$bashbot start $mode" # >/dev/null 2>&1 </dev/null
RETVAL=$?
;;
'starthook')
# shellcheck disable=SC2250
$runcmd "$webhook $mode" # >/dev/null 2>&1 </dev/null
RETVAL=$?
;;
'stop')
# shellcheck disable=SC2250
2021-01-10 15:01:52 +00:00
$runcmd "$bashbot stop $mode"
RETVAL=$?
;;
'stophook')
# shellcheck disable=SC2250
pkill -u "${runas}" "process_batch.sh"
RETVAL=$?
;;
'status')
ps -f -u "${runas}" | grep "${name}" | grep -qF "process_batch.sh --start --watch"
if [ "$?" = "0" ]; then
printf "bashbot (%s) is running in poll mode\n" "${name}"
RETVAL=0
else
ps -f -u "${runas}" | grep "${name}" | grep -qF ""
if [ "$?" = "0" ]; then
printf "bashbot (%s) is running in webhook mode\n" "${name}"
RETVAL=0
else
printf "bashbot (%s) is stopped\n" "${name}"
RETVAL=1
fi
fi
;;
'restart'|'reload')
$0 stop; $0 start
RETVAL=$?
;;
'restarthook'|'reloadhook')
$0 stophook; $0 starthook
RETVAL=$?
;;
2019-04-27 13:48:03 +00:00
'restartback')
$0 suspendback; $0 resumeback
RETVAL=$?
;;
'suspendback'|'resumeback'|'killback')
# shellcheck disable=SC2250
2021-01-10 15:01:52 +00:00
$runcmd "$bashbot $1 $mode"
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
2020-05-14 11:04:57 +00:00
fi
;;
*)
printf "%s\n" "Usage: $0 { start | stop | starthook | restart | reload | restartback | suspendback | resumeback | killback }"
RETVAL=1
;;
esac
exit "${RETVAL}"