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)
|
|
|
|
#
|
2019-06-03 15:04:19 +00:00
|
|
|
#### $$VERSION$$ v0.91-0-g31808a9
|
2019-04-24 08:07:46 +00:00
|
|
|
|
|
|
|
# source from commands.sh if you want ro use interactive or background jobs
|
|
|
|
|
|
|
|
######
|
|
|
|
# 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"
|
|
|
|
}
|
|
|
|
|
|
|
|
# inline and backgound functions
|
|
|
|
# $1 chatid
|
|
|
|
# $2 program
|
|
|
|
# $3 jobname
|
|
|
|
start_back() {
|
2019-05-28 18:44:40 +00:00
|
|
|
local fifo; fifo="${DATADIR:-.}/$(procname "$1")"
|
2019-05-20 08:50:51 +00:00
|
|
|
printf '%s\n' "$1:$3:$2" >"${fifo}$3-back.cmd"
|
2019-05-18 18:25:18 +00:00
|
|
|
start_proc "$1" "$2" "back-$3-"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# $1 chatid
|
|
|
|
# $2 program
|
|
|
|
# $3 prefix
|
|
|
|
start_proc() {
|
|
|
|
[ "$2" = "" ] && return
|
2019-05-20 10:12:11 +00:00
|
|
|
[ -x "${2%% *}" ] || return 1
|
2019-05-28 18:44:40 +00:00
|
|
|
local fifo; fifo="${DATADIR:-.}/$(procname "$1" "$3")"
|
2019-05-19 11:59:36 +00:00
|
|
|
kill_proc "$1" "$3"
|
2019-05-18 19:06:29 +00:00
|
|
|
mkfifo "${fifo}"
|
2019-05-20 13:43:37 +00:00
|
|
|
nohup bash -c "{ tail -f < \"${fifo}\" | $2 \"\" \"\" \"$fifo\" | \"${SCRIPT}\" outproc \"${1}\" \"${fifo}\"
|
|
|
|
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() {
|
2019-05-19 08:52:44 +00:00
|
|
|
[ "$(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
|
|
|
|
[ "${prid}" != "" ] && kill ${prid}
|
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
|
|
|
}
|
|
|
|
|
2019-05-18 18:25:18 +00:00
|
|
|
# $1 chat
|
|
|
|
# $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")"
|
2019-05-20 08:50:51 +00:00
|
|
|
[ -p "${fifo}" ] && printf '%s\n' "$2" >"${fifo}" & # not blocking!
|
|
|
|
}
|
|
|
|
|
|
|
|
# old style but may not work because of local checks
|
|
|
|
inproc() {
|
|
|
|
send_interactive "${CHAT[ID]}" "${MESSAGE}"
|
2019-04-24 08:07:46 +00:00
|
|
|
}
|
2019-05-20 19:40:14 +00:00
|
|
|
|
|
|
|
# start stopp all jobs
|
|
|
|
# $1 command
|
|
|
|
# killb*
|
|
|
|
# suspendb*
|
|
|
|
# resumeb*
|
|
|
|
job_control() {
|
2019-05-21 08:53:52 +00:00
|
|
|
local content proc CHAT job fifo killall=""
|
2019-05-28 18:44:40 +00:00
|
|
|
for FILE in "${DATADIR:-.}/"*-back.cmd; do
|
|
|
|
[ "${FILE}" = "${DATADIR:-.}/*-back.cmd" ] && echo -e "${RED}No background processes.${NC}" && break
|
2019-05-20 19:40:14 +00:00
|
|
|
content="$(< "${FILE}")"
|
|
|
|
CHAT="${content%%:*}"
|
|
|
|
job="${content#*:}"
|
|
|
|
proc="${job#*:}"
|
|
|
|
job="back-${job%:*}-"
|
|
|
|
fifo="$(procname "${CHAT}" "${job}")"
|
|
|
|
case "$1" in
|
|
|
|
"resumeb"*|"backgr"*)
|
|
|
|
echo "Restart Job: ${proc} ${fifo}"
|
|
|
|
start_proc "${CHAT}" "${proc}" "${job}"
|
|
|
|
;;
|
|
|
|
"suspendb"*)
|
|
|
|
echo "Suspend Job: ${proc} ${fifo}"
|
2019-05-21 08:53:52 +00:00
|
|
|
kill_proc "${CHAT}" "${job}"
|
|
|
|
killall="y"
|
2019-05-20 19:40:14 +00:00
|
|
|
;;
|
|
|
|
"killb"*)
|
|
|
|
echo "Kill Job: ${proc} ${fifo}"
|
2019-05-21 08:53:52 +00:00
|
|
|
kill_proc "${CHAT}" "${job}"
|
2019-05-20 19:40:14 +00:00
|
|
|
rm -f "${FILE}" # remove job
|
2019-05-21 08:53:52 +00:00
|
|
|
killall="y"
|
2019-05-20 19:40:14 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
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
|
|
|
}
|