fix background execution

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-18 21:06:29 +02:00
parent bccd064516
commit a1a823b4b6
3 changed files with 18 additions and 13 deletions

View File

@ -12,7 +12,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.80-dev3-0-g31a5d00
#### $$VERSION$$ v0.80-dev3-1-gbccd064
#
# Exit Codes:
# - 0 sucess (hopefully)
@ -424,12 +424,11 @@ if [ "$1" != "source" ]; then
"outproc") # forward output from interactive and jobs to chat
[ "$3" = "" ] && echo "No file to read from" && exit 3
[ "$2" = "" ] && echo "No chat to send to" && exit 3
while true ;do
line=""
read -r -t 10 line
while read -r -t 10 line ;do
[ "$line" != "" ] && send_message "$2" "$line"
done
rm -f -r "${TMPDIR:-.}/$3"
[ -s "${TMPDIR:-.}/$3.log" ] || rm -f "${TMPDIR:-.}/$3.log"
exit
;;
"startbot" )

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.80-dev3-0-g31a5d00
#### $$VERSION$$ v0.80-dev3-1-gbccd064
# adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment
@ -26,6 +26,7 @@ until [ "$SUCCESS" = "y" ] ;do
case $answer in
'Yass!') echo "Goody! mykeyboardendshere";SUCCESS=y;;
'No') echo "Well that's weird. mykeyboardendshere";SUCCESS=y;;
'') echo "empty answer!" && exit;;
*) SUCCESS=n;;
esac
done

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.80-dev3-0-g31a5d00
#### $$VERSION$$ v0.80-dev3-1-gbccd064
# source from commands.sh if you want ro use interactive or background jobs
@ -50,8 +50,8 @@ listproc() {
# $2 program
# $3 jobname
start_back() {
local fifo; fifo="$(fifoname "$1")"
echo "$1:$3:$2" >"${TMPDIR:-.}/${fifo}$3-back.cmd"
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")"
echo "$1:$3:$2" >"${fifo}$3-back.cmd"
start_proc "$1" "$2" "back-$3-"
}
@ -62,9 +62,12 @@ start_back() {
start_proc() {
[ "$2" = "" ] && return
kill_proc "$1" "$3"
local fifo; fifo="$(fifoname "$1" "$3")"
mkfifo "${TMPDIR:-.}/${fifo}"
( $2 <"${TMPDIR:-.}/${fifo}" | "${SCRIPT}" outproc "${1}" "${fifo}"; ) &>>"${TMPDIR:-.}/${fifo}.log" &
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1" "$3")"
mkfifo "${fifo}"
{ set -f
# shellcheck disable=SC2002
cat "${fifo}" | $2 | "${SCRIPT}" outproc "${1}" "${fifo}"
} &>>"${fifo}.log" &
disown -a
}
@ -96,12 +99,14 @@ kill_back() {
kill_proc() {
local fifo; fifo="$(fifoname "$1" "$2")"
kill -15 "$(listproc "${fifo}")" 2>/dev/null
rm -f -r "${TMPDIR:-.}/${fifo}";
fifo="${TMPDIR:-.}/${fifo}"
[ -s "${fifo}.log" ] || rm -f "${fifo}.log"
[ -p "${fifo}" ] && rm -f "${fifo}";
}
# $1 chat
# $2 message
forward_interactive() {
local fifo; fifo="$(fifoname "$1")"
local fifo; fifo="${TMPDIR:-.}/$(fifoname "$1")"
[ -p "${fifo}" ] && echo "$2" >"${fifo}"
}