start, kill, resume, suspend now working

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-19 15:03:44 +02:00
parent 83623ece2e
commit beb77a4b65
5 changed files with 64 additions and 38 deletions

View File

@ -18,7 +18,7 @@
<p>Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.</p> <p>Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.</p>
<p>Released to the public domain wherever applicable. Elsewhere, consider it released under the <a href="http://www.wtfpl.net/txt/copying/">WTFPLv2</a>.</p> <p>Released to the public domain wherever applicable. Elsewhere, consider it released under the <a href="http://www.wtfpl.net/txt/copying/">WTFPLv2</a>.</p>
<h2 id="prerequisites">Prerequisites</h2> <h2 id="prerequisites">Prerequisites</h2>
<p>Depends on <a href="http://github.com/tmux/tmux">tmux</a>. Uses <a href="http://github.com/dominictarr/JSON.sh">JSON.sh</a>.</p> <p>Uses <a href="http://github.com/dominictarr/JSON.sh">JSON.sh</a>, but no more TMUX.</p>
<p>Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. More concret on the common commands provided by <a href="https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands">coreutils</a>, <a href="https://en.wikipedia.org/wiki/BusyBox#Commands">busybox</a> or <a href="https://landley.net/toybox/help.html">toybox</a>, see <a href="doc/7_develop.md#common-commands">Developer Notes</a></p> <p>Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. More concret on the common commands provided by <a href="https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands">coreutils</a>, <a href="https://en.wikipedia.org/wiki/BusyBox#Commands">busybox</a> or <a href="https://landley.net/toybox/help.html">toybox</a>, see <a href="doc/7_develop.md#common-commands">Developer Notes</a></p>
<p>Bashbot <a href="https://github.com/topkecleon/telegram-bot-bash">Documentation</a> and <a href="https://github.com/topkecleon/telegram-bot-bash/releases">Downloads</a> are availible on www.github.com</p> <p>Bashbot <a href="https://github.com/topkecleon/telegram-bot-bash">Documentation</a> and <a href="https://github.com/topkecleon/telegram-bot-bash/releases">Downloads</a> are availible on www.github.com</p>
<h2 id="documentation">Documentation</h2> <h2 id="documentation">Documentation</h2>
@ -107,6 +107,6 @@
<p><span class="citation">@Gnadelwartz</span></p> <p><span class="citation">@Gnadelwartz</span></p>
<h2 id="thats-it">Thats it!</h2> <h2 id="thats-it">Thats it!</h2>
<p>If you feel that theres something missing or if you found a bug, feel free to submit a pull request!</p> <p>If you feel that theres something missing or if you found a bug, feel free to submit a pull request!</p>
<h4 id="version-v0.80-dev3-0-g31a5d00"><br /><span class="math display"><em>V</em><em>E</em><em>R</em><em>S</em><em>I</em><em>O</em><em>N</em></span><br /> v0.80-dev3-0-g31a5d00</h4> <h4 id="version-v0.80-dev3-5-g83623ec"><br /><span class="math display"><em>V</em><em>E</em><em>R</em><em>S</em><em>I</em><em>O</em><em>N</em></span><br /> v0.80-dev3-5-g83623ec</h4>
</body> </body>
</html> </html>

View File

@ -9,8 +9,7 @@ Released to the public domain wherever applicable.
Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/).
## Prerequisites ## Prerequisites
Depends on [tmux](http://github.com/tmux/tmux). Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX.
Uses [JSON.sh](http://github.com/dominictarr/JSON.sh).
Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment. Even bashbot is written in bash, it depends on commands typically availible in a Unix/Linux Environment.
More concret on the common commands provided by [coreutils](https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands), [busybox](https://en.wikipedia.org/wiki/BusyBox#Commands) or [toybox](https://landley.net/toybox/help.html), see [Developer Notes](doc/7_develop.md#common-commands) More concret on the common commands provided by [coreutils](https://en.wikipedia.org/wiki/List_of_GNU_Core_Utilities_commands), [busybox](https://en.wikipedia.org/wiki/BusyBox#Commands) or [toybox](https://landley.net/toybox/help.html), see [Developer Notes](doc/7_develop.md#common-commands)
@ -107,4 +106,4 @@ Well, thats a damn good question ... may be because I'm an Unix/Linux admin from
If you feel that there's something missing or if you found a bug, feel free to submit a pull request! If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
#### $$VERSION$$ v0.80-dev3-0-g31a5d00 #### $$VERSION$$ v0.80-dev3-5-g83623ec

View File

@ -13,8 +13,7 @@ Elsewhere, consider it released under the
[WTFPLv2](http://www.wtfpl.net/txt/copying/). [WTFPLv2](http://www.wtfpl.net/txt/copying/).
## Prerequisites ## Prerequisites
Depends on [tmux](http://github.com/tmux/tmux). Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX.
Uses [JSON.sh](http://github.com/dominictarr/JSON.sh).
Even bashbot is written in bash, it depends on commands typically availible in Even bashbot is written in bash, it depends on commands typically availible in
a Unix/Linux Environment. a Unix/Linux Environment.
@ -155,4 +154,4 @@ health status
If you feel that there's something missing or if you found a bug, feel free to If you feel that there's something missing or if you found a bug, feel free to
submit a pull request! submit a pull request!
#### $$VERSION$$ v0.80-dev3-0-g31a5d00 #### $$VERSION$$ v0.80-dev3-5-g83623ec

View File

@ -12,7 +12,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.80-dev3-4-ge7d2eff #### $$VERSION$$ v0.80-dev3-5-g83623ec
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)
@ -392,7 +392,7 @@ bot_init() {
[ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${TMPDIR}"; rmdir "${OLDTMP}"; } [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${TMPDIR}"; rmdir "${OLDTMP}"; }
[ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh"
#setup bashbot #setup bashbot
[[ "$(id -u)" -eq "0" ]] && RUNUSER="nobody" [[ "${UID}" -eq "0" ]] && RUNUSER="nobody"
echo -n "Enter User to run basbot [$RUNUSER]: " echo -n "Enter User to run basbot [$RUNUSER]: "
read -r TOUSER read -r TOUSER
[ "$TOUSER" = "" ] && TOUSER="$RUNUSER" [ "$TOUSER" = "" ] && TOUSER="$RUNUSER"
@ -460,15 +460,14 @@ if [ "$1" != "source" ]; then
bot_init bot_init
exit exit
;; ;;
"attach")
tmux attach -t "$ME"
exit
;;
esac esac
############### ###############
# "official" arguments as shown to users # "official" arguments as shown to users
SESSION="$ME-startbot"
BOTPID="$(proclist "${SESSION}")"
case "$1" in case "$1" in
"count") "count")
echo "A total of $(wc -l <"${COUNTFILE}") users used me." echo "A total of $(wc -l <"${COUNTFILE}") users used me."
@ -481,46 +480,75 @@ if [ "$1" != "source" ]; then
shift shift
while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}" while read -r f; do send_markdown_message "${f//COUNT}" "$*"; $sleep; done <"${COUNTFILE}"
;; ;;
"status")
if [ "${BOTPID}" != "" ]; then
echo -e "${GREEN}Bot is running.${NC}"
exit
else
echo -e "${ORANGE}Bot not running.${NC}"
exit 5
fi
;;
"start") "start")
tmux kill-session -t "$ME" &>/dev/null # shellcheck disable=SC2086
tmux new-session -d -s "$ME" "bash $SCRIPT startbot" && echo -e "${GREEN}Bot started successfully.${NC}" [ "${BOTPID}" != "" ] && kill ${BOTPID}
echo "Tmux session name $ME" || echo -e "${RED}An error occurred while starting the bot. ${NC}" nohup "$SCRIPT" "startbot" "$2" "${SESSION}" &>/dev/null &
echo "Session Name: ${SESSION}"
if [ "$(proclist "${SESSION}")" != "" ]; then
echo -e "${GREEN}Bot started successfully.${NC}"
else
echo -e "${RED}An error occurred while starting the bot.${NC}"
exit 5
fi
;; ;;
"kill") "kill"|"stop")
tmux kill-session -t "$ME" &>/dev/null if [ "${BOTPID}" != "" ]; then
echo -e "${GREEN}OK. Bot stopped successfully.${NC}" # shellcheck disable=SC2086
if kill ${BOTPID}; then
echo -e "${GREEN}OK. Bot stopped successfully.${NC}"
else
echo -e "${RED}An error occured while stopping bot.${NC}"
exit 5
fi
fi
exit
;; ;;
"background" | "resumeback") "backgr"* | "resumeb"*)
echo -e "${GREEN}Restart background processes ...${NC}" echo -e "${GREEN}Restart background processes ...${NC}"
for FILE in "${TMPDIR:-.}/"*-back.cmd; do for FILE in "${TMPDIR:-.}/"*-back.cmd; do
if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then
echo -e "${RED}No background processes to start.${NC}"; break echo -e "${RED}No background processes to start.${NC}"; break
else else
RESTART="$(< "${FILE}")" CONTENT="$(< "${FILE}")"
CHAT[ID]="${RESTART%%:*}" CHAT[ID]="${CONTENT%%:*}"
JOB="${RESTART#*:}" JOB="${CONTENT#*:}"
PROG="${JOB#*:}" PROG="${JOB#*:}"
JOB="${JOB%:*}" JOB="${JOB%:*}"
fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")"
echo "restartbackground ${PROG} ${fifo}" echo "restart background job ${PROG} ${fifo}"
start_back "${CHAT[ID]}" "${PROG}" "${JOB}" start_back "${CHAT[ID]}" "${PROG}" "${JOB}"
fi fi
done done
;; ;;
"killback" | "suspendback") "killb"* | "suspendb"*)
echo -e "${GREEN}Stopping background processes ...${NC}" echo -e "${GREEN}Stopping background processes ...${NC}"
for FILE in "${TMPDIR:-.}/"*-back.cmd; do for FILE in "${TMPDIR:-.}/"*-back.cmd; do
if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then
echo -e "${RED}No background processes.${NC}"; break echo -e "${RED}No background processes.${NC}"; break
else else
REMOVE="$(< "${FILE}")" CONTENT="$(< "${FILE}")"
CHAT[ID]="${RESTART%%:*}" CHAT[ID]="${CONTENT%%:*}"
JOB="${REMOVE#*:}" JOB="${CONTENT#*:}"
JOB="${JOB%:*}" JOB="${JOB%:*}"
fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")"
echo "killbackground ${fifo}" if [[ "$1" = "killb"* ]]; then
[ "$1" = "killback" ] && rm -f "${FILE}" # remove job rm -f "${FILE}" # remove job
kill_proc "${CHAT[ID]}" "back-${JOB}" echo "kill background job ${fifo}"
else
echo "suspend background job ${fifo}"
fi
kill_proc "${CHAT[ID]}" "back-${JOB}-"
fi fi
done done
;; ;;
@ -530,13 +558,13 @@ if [ "$1" != "source" ]; then
;; ;;
*) *)
echo -e "${RED}${ME}: BAD REQUEST${NC}" echo -e "${RED}${ME}: BAD REQUEST${NC}"
echo -e "${RED}Available arguments: start, kill, count, broadcast, help, suspendback, resumeback, killback${NC}" echo -e "${RED}Available arguments: start, stop, kill, status, count, broadcast, help, suspendback, resumeback, killback${NC}"
exit 4 exit 4
;; ;;
esac esac
# warn if root # warn if root
if [[ "$(id -u)" -eq "0" ]] ; then if [[ "${UID}" -eq "0" ]] ; then
echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}" echo -e "\\n${ORANGE}WARNING: ${SCRIPT} was started as ROOT (UID 0)!${NC}"
echo -e "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}" echo -e "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}"
fi fi

View File

@ -5,7 +5,7 @@ This section is about help and best practices for new bashbot developers. The ma
bashbot development is done on github. If you want to provide fixes or new features [fork bashbot on githup](https://help.github.com/en/articles/fork-a-repo) and provide changes as [pull request on github](https://help.github.com/en/articles/creating-a-pull-request). bashbot development is done on github. If you want to provide fixes or new features [fork bashbot on githup](https://help.github.com/en/articles/fork-a-repo) and provide changes as [pull request on github](https://help.github.com/en/articles/creating-a-pull-request).
### Debugging Bashbot ### Debugging Bashbot
In normal mode of operation all bashbot output is discarded one more correct sent to TMUX console. In normal mode of operation all bashbot output is discarded.
To get these messages (and more) you can start bashbot in the current shell ```./bashbot.sh startbot```. Now you can see all output or erros from bashbot. To get these messages (and more) you can start bashbot in the current shell ```./bashbot.sh startbot```. Now you can see all output or erros from bashbot.
In addition you can change the change the level of verbosity by adding a third argument after startbot. In addition you can change the change the level of verbosity by adding a third argument after startbot.
``` ```
@ -188,5 +188,5 @@ fi
#### [Prev Function Reference](6_reference.md) #### [Prev Function Reference](6_reference.md)
#### [Next Bashbot Environment](8_custom.md) #### [Next Bashbot Environment](8_custom.md)
#### $$VERSION$$ v0.80-dev3-0-g31a5d00 #### $$VERSION$$ v0.80-dev3-5-g83623ec