2019-04-24 08:07:46 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# file: modules/background.sh
|
|
|
|
# do not edit, this file will be overwritten on update
|
|
|
|
|
|
|
|
# This file is public domain in the USA and all free countries.
|
|
|
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
|
|
|
#
|
2020-12-26 20:29:16 +00:00
|
|
|
# shellcheck disable=SC1117,SC2059
|
2021-06-03 12:21:40 +00:00
|
|
|
#### $$VERSION$$ v1.51-0-g6e66a28
|
2019-04-24 08:07:46 +00:00
|
|
|
|
2020-06-14 18:56:46 +00:00
|
|
|
# will be automatically sourced from bashbot
|
2019-04-24 08:07:46 +00:00
|
|
|
|
2020-05-14 17:47:37 +00:00
|
|
|
# source once magic, function named like file
|
|
|
|
eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
|
|
|
|
|
2019-04-24 08:07:46 +00:00
|
|
|
######
|
|
|
|
# interactive and background functions
|
|
|
|
|
2019-05-18 18:25:18 +00:00
|
|
|
# old syntax as aliases
|
2019-04-24 08:07:46 +00:00
|
|
|
background() {
|
2019-05-18 18:25:18 +00:00
|
|
|
start_back "${CHAT[ID]}" "$1" "$2"
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
|
|
|
startproc() {
|
2019-05-18 18:25:18 +00:00
|
|
|
start_proc "${CHAT[ID]}" "$1" "$2"
|
|
|
|
}
|
|
|
|
checkback() {
|
|
|
|
check_back "${CHAT[ID]}" "$1"
|
|
|
|
}
|
|
|
|
checkproc() {
|
|
|
|
check_proc "${CHAT[ID]}" "$1"
|
|
|
|
}
|
|
|
|
killback() {
|
|
|
|
kill_back "${CHAT[ID]}" "$1"
|
|
|
|
}
|
|
|
|
killproc() {
|
|
|
|
kill_proc "${CHAT[ID]}" "$1"
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:35:50 +00:00
|
|
|
# inline and background functions
|
2019-05-18 18:25:18 +00:00
|
|
|
# $1 chatid
|
|
|
|
# $2 program
|
|
|
|
# $3 jobname
|
2020-06-06 21:01:11 +00:00
|
|
|
# $4 $5 parameters
|
2019-05-18 18:25:18 +00:00
|
|
|
start_back() {
|
2020-06-18 10:39:07 +00:00
|
|
|
local cmdfile; cmdfile="${DATADIR:-.}/$(procname "$1")$3-back.cmd"
|
|
|
|
printf '%s\n' "$1:$3:$2" >"${cmdfile}"
|
2020-06-06 21:01:11 +00:00
|
|
|
restart_back "$@"
|
|
|
|
}
|
2021-02-24 15:33:03 +00:00
|
|
|
# $1 chatid
|
|
|
|
# $2 program
|
|
|
|
# $3 jobname
|
|
|
|
# $4 $5 parameters
|
2020-06-06 21:01:11 +00:00
|
|
|
restart_back() {
|
2020-06-07 07:10:02 +00:00
|
|
|
local fifo; fifo="${DATADIR:-.}/$(procname "$1" "back-$3-")"
|
2021-02-04 17:13:32 +00:00
|
|
|
log_update "Start background job CHAT=$1 JOB=${fifo##*/} CMD=${2##*/} $4 $5"
|
2020-06-20 08:16:12 +00:00
|
|
|
check_back "$1" "$3" && kill_proc "$1" "back-$3-"
|
2021-01-05 15:17:34 +00:00
|
|
|
nohup bash -c "{ $2 \"$4\" \"$5\" \"${fifo}\" | \"${SCRIPT}\" outproc \"$1\" \"${fifo}\"; }" &>>"${fifo}.log" &
|
2021-01-20 18:50:19 +00:00
|
|
|
sleep 0.5 # give bg job some time to init
|
2019-05-18 18:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# $1 chatid
|
|
|
|
# $2 program
|
2020-06-07 07:31:03 +00:00
|
|
|
# $3 $4 parameters
|
2019-05-18 18:25:18 +00:00
|
|
|
start_proc() {
|
2020-05-14 18:33:30 +00:00
|
|
|
[ -z "$2" ] && return
|
2019-05-20 10:12:11 +00:00
|
|
|
[ -x "${2%% *}" ] || return 1
|
2020-06-07 07:31:03 +00:00
|
|
|
local fifo; fifo="${DATADIR:-.}/$(procname "$1")"
|
2020-06-20 08:16:12 +00:00
|
|
|
check_proc "$1" && kill_proc "$1"
|
2019-05-18 19:06:29 +00:00
|
|
|
mkfifo "${fifo}"
|
2021-02-24 15:33:03 +00:00
|
|
|
log_update "Start interactive script CHAT=$1 JOB=${fifo##*/} CMD=$2 $3 $4"
|
2021-01-05 15:17:34 +00:00
|
|
|
nohup bash -c "{ $2 \"$4\" \"$5\" \"${fifo}\" | \"${SCRIPT}\" outproc \"$1\" \"${fifo}\"
|
2020-06-07 11:30:59 +00:00
|
|
|
rm \"${fifo}\"; [ -s \"${fifo}.log\" ] || rm -f \"${fifo}.log\"; }" &>>"${fifo}.log" &
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-18 18:25:18 +00:00
|
|
|
# $1 chatid
|
|
|
|
# $2 jobname
|
|
|
|
check_back() {
|
|
|
|
check_proc "$1" "back-$2-"
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 18:25:18 +00:00
|
|
|
# $1 chatid
|
|
|
|
# $2 prefix
|
|
|
|
check_proc() {
|
2020-05-14 18:33:30 +00:00
|
|
|
[ -n "$(proclist "$(procname "$1" "$2")")" ]
|
2019-05-12 15:51:52 +00:00
|
|
|
# shellcheck disable=SC2034
|
|
|
|
res=$?; return $?
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 18:25:18 +00:00
|
|
|
# $1 chatid
|
|
|
|
# $2 jobname
|
|
|
|
kill_back() {
|
|
|
|
kill_proc "$1" "back-$2-"
|
2019-05-28 18:44:40 +00:00
|
|
|
rm -f "${DATADIR:-.}/$(procname "$1")$2-back.cmd"
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 18:25:18 +00:00
|
|
|
|
|
|
|
# $1 chatid
|
|
|
|
# $2 prefix
|
|
|
|
kill_proc() {
|
2019-05-19 10:54:59 +00:00
|
|
|
local fifo prid
|
|
|
|
fifo="$(procname "$1" "$2")"
|
|
|
|
prid="$(proclist "${fifo}")"
|
2019-05-28 18:44:40 +00:00
|
|
|
fifo="${DATADIR:-.}/${fifo}"
|
2019-05-19 10:54:59 +00:00
|
|
|
# shellcheck disable=SC2086
|
2021-02-24 15:33:03 +00:00
|
|
|
if [ -n "${prid}" ]; then
|
|
|
|
log_update "Stop interactive / background CHAT=$1 JOB=${fifo##*/}"
|
|
|
|
kill ${prid}
|
|
|
|
fi
|
2019-05-18 19:06:29 +00:00
|
|
|
[ -s "${fifo}.log" ] || rm -f "${fifo}.log"
|
|
|
|
[ -p "${fifo}" ] && rm -f "${fifo}";
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 07:31:03 +00:00
|
|
|
# $1 chatid
|
2019-05-18 18:25:18 +00:00
|
|
|
# $2 message
|
2019-05-20 08:50:51 +00:00
|
|
|
send_interactive() {
|
2019-05-28 18:44:40 +00:00
|
|
|
local fifo; fifo="${DATADIR:-.}/$(procname "$1")"
|
2021-01-20 18:50:19 +00:00
|
|
|
[ -p "${fifo}" ] && printf '%s\n' "$2" >"${fifo}" & # not blocking!
|
2019-05-20 08:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# old style but may not work because of local checks
|
|
|
|
inproc() {
|
2020-06-28 21:02:58 +00:00
|
|
|
send_interactive "${CHAT[ID]}" "${MESSAGE[0]}"
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
2019-05-20 19:40:14 +00:00
|
|
|
|
2021-03-04 12:58:04 +00:00
|
|
|
# start stop all jobs
|
2021-02-25 18:58:00 +00:00
|
|
|
# $1 command # kill suspend resume restart
|
2019-05-20 19:40:14 +00:00
|
|
|
job_control() {
|
2020-06-16 05:03:47 +00:00
|
|
|
local BOT ADM content proc CHAT job fifo killall=""
|
|
|
|
BOT="$(getConfigKey "botname")"
|
2021-03-03 12:27:07 +00:00
|
|
|
ADM="${BOTADMIN}"
|
2021-01-05 15:17:34 +00:00
|
|
|
debug_checks "Enter job_control" "$1"
|
2021-03-04 12:58:04 +00:00
|
|
|
# cleanup on start
|
|
|
|
[[ "$1" == "re"* ]] && bot_cleanup "startback"
|
2019-05-28 18:44:40 +00:00
|
|
|
for FILE in "${DATADIR:-.}/"*-back.cmd; do
|
2020-12-26 20:29:16 +00:00
|
|
|
[ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && printf "${RED}No background processes.${NN}" && break
|
2019-05-20 19:40:14 +00:00
|
|
|
content="$(< "${FILE}")"
|
|
|
|
CHAT="${content%%:*}"
|
|
|
|
job="${content#*:}"
|
|
|
|
proc="${job#*:}"
|
2020-06-06 21:01:11 +00:00
|
|
|
job="${job%:*}"
|
2019-05-20 19:40:14 +00:00
|
|
|
fifo="$(procname "${CHAT}" "${job}")"
|
2021-01-05 15:17:34 +00:00
|
|
|
debug_checks "Execute job_control" "$1" "${FILE##*/}"
|
2019-05-20 19:40:14 +00:00
|
|
|
case "$1" in
|
2021-02-25 18:58:00 +00:00
|
|
|
"resume"*|"restart"*)
|
2020-07-01 10:07:00 +00:00
|
|
|
printf "Restart Job: %s %s\n" "${proc}" " ${fifo##*/}"
|
2020-06-06 21:01:11 +00:00
|
|
|
restart_back "${CHAT}" "${proc}" "${job}"
|
2020-06-15 18:07:43 +00:00
|
|
|
# inform botadmin about stop
|
2020-06-16 05:03:47 +00:00
|
|
|
[ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} restart background jobs ..." &
|
2019-05-20 19:40:14 +00:00
|
|
|
;;
|
2021-02-25 18:58:00 +00:00
|
|
|
"suspend"*)
|
2020-07-01 10:07:00 +00:00
|
|
|
printf "Suspend Job: %s %s\n" "${proc}" " ${fifo##*/}"
|
2019-05-21 08:53:52 +00:00
|
|
|
kill_proc "${CHAT}" "${job}"
|
2020-06-15 18:07:43 +00:00
|
|
|
# inform botadmin about stop
|
2020-06-16 05:03:47 +00:00
|
|
|
[ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} suspend background jobs ..." &
|
2019-05-21 08:53:52 +00:00
|
|
|
killall="y"
|
2019-05-20 19:40:14 +00:00
|
|
|
;;
|
2021-02-25 18:58:00 +00:00
|
|
|
"kill"*)
|
2020-07-01 10:07:00 +00:00
|
|
|
printf "Kill Job: %s %s\n" "${proc}" " ${fifo##*/}"
|
2019-05-21 08:53:52 +00:00
|
|
|
kill_proc "${CHAT}" "${job}"
|
2021-01-20 18:50:19 +00:00
|
|
|
rm -f "${FILE}" # remove job
|
2020-06-15 18:07:43 +00:00
|
|
|
# inform botadmin about stop
|
2020-06-16 05:03:47 +00:00
|
|
|
[ -n "${ADM}" ] && send_normal_message "${ADM}" "Bot ${BOT} kill background jobs ..." &
|
2019-05-21 08:53:52 +00:00
|
|
|
killall="y"
|
2019-05-20 19:40:14 +00:00
|
|
|
;;
|
|
|
|
esac
|
2020-06-16 05:03:47 +00:00
|
|
|
# send message only onnfirst job
|
|
|
|
ADM=""
|
2019-05-20 19:40:14 +00:00
|
|
|
done
|
2021-01-05 15:17:34 +00:00
|
|
|
debug_checks "end job_control" "$1"
|
2019-05-21 08:53:52 +00:00
|
|
|
# kill all requestet. kill ALL background jobs, even not listed in data-bot-bash
|
|
|
|
[ "${killall}" = "y" ] && killallproc "back-"
|
2019-05-20 19:40:14 +00:00
|
|
|
}
|