From beb77a4b65001e6eb7f294dfcca164d2e4433640 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 19 May 2019 15:03:44 +0200 Subject: [PATCH] start, kill, resume, suspend now working --- README.html | 4 +-- README.md | 5 ++- README.txt | 5 ++- bashbot.sh | 84 ++++++++++++++++++++++++++++++++---------------- doc/7_develop.md | 4 +-- 5 files changed, 64 insertions(+), 38 deletions(-) diff --git a/README.html b/README.html index ae2c52f..3c3f61e 100644 --- a/README.html +++ b/README.html @@ -18,7 +18,7 @@

Contributions by JuanPotato, BigNerd95, TiagoDanin, and iicc1.

Released to the public domain wherever applicable. Elsewhere, consider it released under the WTFPLv2.

Prerequisites

-

Depends on tmux. Uses JSON.sh.

+

Uses JSON.sh, but no more TMUX.

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, busybox or toybox, see Developer Notes

Bashbot Documentation and Downloads are availible on www.github.com

Documentation

@@ -107,6 +107,6 @@

@Gnadelwartz

That’s it!

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

diff --git a/README.md b/README.md index c4184ec..cc2d1f9 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,7 @@ Released to the public domain wherever applicable. Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). ## Prerequisites -Depends on [tmux](http://github.com/tmux/tmux). -Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). +Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX. 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) @@ -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! -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-5-g83623ec diff --git a/README.txt b/README.txt index f394c59..d1459ff 100644 --- a/README.txt +++ b/README.txt @@ -13,8 +13,7 @@ Elsewhere, consider it released under the [WTFPLv2](http://www.wtfpl.net/txt/copying/). ## Prerequisites -Depends on [tmux](http://github.com/tmux/tmux). -Uses [JSON.sh](http://github.com/dominictarr/JSON.sh). +Uses [JSON.sh](http://github.com/dominictarr/JSON.sh), but no more TMUX. Even bashbot is written in bash, it depends on commands typically availible in 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 submit a pull request! -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-5-g83623ec diff --git a/bashbot.sh b/bashbot.sh index 21c3497..af65f72 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -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-4-ge7d2eff +#### $$VERSION$$ v0.80-dev3-5-g83623ec # # Exit Codes: # - 0 sucess (hopefully) @@ -392,7 +392,7 @@ bot_init() { [ -d "${OLDTMP}" ] && { mv -n "${OLDTMP}/"* "${TMPDIR}"; rmdir "${OLDTMP}"; } [ -f "modules/inline.sh" ] && rm -f "modules/inline.sh" #setup bashbot - [[ "$(id -u)" -eq "0" ]] && RUNUSER="nobody" + [[ "${UID}" -eq "0" ]] && RUNUSER="nobody" echo -n "Enter User to run basbot [$RUNUSER]: " read -r TOUSER [ "$TOUSER" = "" ] && TOUSER="$RUNUSER" @@ -460,15 +460,14 @@ if [ "$1" != "source" ]; then bot_init exit ;; - "attach") - tmux attach -t "$ME" - exit - ;; esac ############### # "official" arguments as shown to users + SESSION="$ME-startbot" + BOTPID="$(proclist "${SESSION}")" + case "$1" in "count") echo "A total of $(wc -l <"${COUNTFILE}") users used me." @@ -481,46 +480,75 @@ if [ "$1" != "source" ]; then shift 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") - tmux kill-session -t "$ME" &>/dev/null - tmux new-session -d -s "$ME" "bash $SCRIPT startbot" && echo -e "${GREEN}Bot started successfully.${NC}" - echo "Tmux session name $ME" || echo -e "${RED}An error occurred while starting the bot. ${NC}" + # shellcheck disable=SC2086 + [ "${BOTPID}" != "" ] && kill ${BOTPID} + 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") - tmux kill-session -t "$ME" &>/dev/null - echo -e "${GREEN}OK. Bot stopped successfully.${NC}" + "kill"|"stop") + if [ "${BOTPID}" != "" ]; then + # 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}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes to start.${NC}"; break else - RESTART="$(< "${FILE}")" - CHAT[ID]="${RESTART%%:*}" - JOB="${RESTART#*:}" + CONTENT="$(< "${FILE}")" + CHAT[ID]="${CONTENT%%:*}" + JOB="${CONTENT#*:}" PROG="${JOB#*:}" JOB="${JOB%:*}" - fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" - echo "restartbackground ${PROG} ${fifo}" + fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")" + echo "restart background job ${PROG} ${fifo}" start_back "${CHAT[ID]}" "${PROG}" "${JOB}" fi done ;; - "killback" | "suspendback") + "killb"* | "suspendb"*) echo -e "${GREEN}Stopping background processes ...${NC}" for FILE in "${TMPDIR:-.}/"*-back.cmd; do if [ "${FILE}" = "${TMPDIR:-.}/*-back.cmd" ]; then echo -e "${RED}No background processes.${NC}"; break else - REMOVE="$(< "${FILE}")" - CHAT[ID]="${RESTART%%:*}" - JOB="${REMOVE#*:}" + CONTENT="$(< "${FILE}")" + CHAT[ID]="${CONTENT%%:*}" + JOB="${CONTENT#*:}" JOB="${JOB%:*}" - fifo="$(fifoname "${CHAT[ID]}" "back-${JOB}")" - echo "killbackground ${fifo}" - [ "$1" = "killback" ] && rm -f "${FILE}" # remove job - kill_proc "${CHAT[ID]}" "back-${JOB}" + fifo="$(procname "${CHAT[ID]}" "back-${JOB}-")" + if [[ "$1" = "killb"* ]]; then + rm -f "${FILE}" # remove job + echo "kill background job ${fifo}" + else + echo "suspend background job ${fifo}" + fi + kill_proc "${CHAT[ID]}" "back-${JOB}-" fi done ;; @@ -530,13 +558,13 @@ if [ "$1" != "source" ]; then ;; *) 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 ;; esac # 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 "${ORANGE}You are at HIGH RISK when processing user input with root privilegs!${NC}" fi diff --git a/doc/7_develop.md b/doc/7_develop.md index 112bf6b..81eed8e 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -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). ### 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. 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) #### [Next Bashbot Environment](8_custom.md) -#### $$VERSION$$ v0.80-dev3-0-g31a5d00 +#### $$VERSION$$ v0.80-dev3-5-g83623ec