From ef571de225d48762aad38f9a5a3b28bbab654e39 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Sat, 23 Nov 2019 18:49:37 -0500 Subject: [PATCH] fix jrun --- .bashrc | 3 +++ brc | 16 +++++++++++++++ brc2 | 62 ++++++++++++++++++++------------------------------------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/.bashrc b/.bashrc index 840a353..d374dd4 100644 --- a/.bashrc +++ b/.bashrc @@ -9,6 +9,7 @@ #exec 2>>/a/tmp/bashlog + # History related options first, or else # we risk screwing up history history. And this is duplicated # in ~/.bash_profile just for good measure @@ -71,5 +72,7 @@ else fi fi fi + + # ensure no bad programs appending to this file will have an affect return 0 diff --git a/brc b/brc index 118ce01..30ee49a 100644 --- a/brc +++ b/brc @@ -3,6 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-or-later # this gets sourced. shebang is just for file mode detection + # Use source ~/.bashrc instead of doing bash -l when running a script # so this can set extdebug and avoid the bash debugger. if [[ -s /a/bin/errhandle/err ]]; then @@ -1274,6 +1275,13 @@ if [[ $- == *i* ]]; then history -a # save history fi + if [[ $jr_pid ]]; then + if [[ -e /proc/$jr_pid ]]; then + kill $jr_pid + fi + unset jr_pid + fi + case $return in 0) ps_color="$term_purple" ps_char='\$' @@ -1338,6 +1346,14 @@ unset IFS # shellcheck disable=SC1090 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function* +# I had this idea to start a bash shell which would run an initial +# command passed through this env variable, then continue on +# interactively. But the use case I had in mind went away. +# +# if [[ $MY_INIT_CMD ]]; then +# "${MY_INIT_CMD[@]}" +# unset MY_INIT_CMD +# fi # ensure no bad programs appending to this file will have an affect return 0 diff --git a/brc2 b/brc2 index 50d51ba..d9c7cbd 100644 --- a/brc2 +++ b/brc2 @@ -154,10 +154,12 @@ inttrap() { fi } -_jrun() { # journal run. run args, log to journal, tail and grep the journal. - # Redirect all commands which might have stderr to stdout because of - # wrapping. - local pid pattern jr_pid sedscript cmd_name ended + +jrun() { # journal run. run args, log to journal, tail and grep the journal. + # Note, an alternative without systemd would be something like ts. + # Note: I tried doing cmd | pee "sudo systemd-cat" cat, but that + # had some problems like ctrl-c didnt work or something. + local pid pattern sedscript cmd_name ended ret=0 case $1 in -p) @@ -166,45 +168,25 @@ _jrun() { # journal run. run args, log to journal, tail and grep the journal. ;; esac cmd_name=${1##*/} - systemd-cat -t "$cmd_name" "$@" 2>&1 & - pid=$! - old_int_trap="$(trap -p INT)" - # Note, just passing along INT wont actually stop it. - # Oddly, the log says ERROR: /script.sh returned 130 - # but it continues on. - trap "inttrap $pid" INT - sedscript="/$pattern$cmd_name/p;/^.{16}[^ ]+ $cmd_name\[$pid]: ([^ ]*\/)?$cmd_name: exiting with status [0-9]+\$/q" - # debug: - #echo "sedscript: sed -nr '$sedscript'" - journalctl -S "4 seconds ago" -f |& sed -nr "$sedscript" 2>&1 & + sedscript="/$pattern$cmd_name/p;/^.{16}[^ ]+ $cmd_name\[[0-9]+]: ([^ ]*\/)?$cmd_name: exiting with status [0-9]+\$/q" + # We use >() so that $! is the pid of journalctl, otherwise its the sed pid and then + # if we kill that, it takes journalctl about 10 seconds to catch up, and we get + # an annoying message about job finishing then. + journalctl -qn2 -f &> >(sed -nr "$sedscript") & + # We kill this in prompt-command for the case that we ctrl-c the + # systemd-cat. i dont know any way to trap ctrl-c and still run the + # normal action for it. There might be a way, unsure. jr_pid=$! - wait $pid 2>&1 || ret=$? + systemd-cat -t "$cmd_name" "$@" || ret=$? if (( $ret )); then - echo "$0: ERROR: $* returned $ret" - fi - ended=false - # give it 4 seconds to find the end of the log - for (( i=0; i<80; i++ )); do - if [[ -e /proc/$jr_pid ]]; then - sleep .05 2>&1 - else - ended=true - break - fi - done - if ! $ended; then - kill $jr_pid 2>&1 + echo "jrun: ERROR: $* returned $ret" fi - if [[ $old_int_trap ]]; then - $old_int_trap - else - trap INT - fi - return 0 -} -jrun() { - # ditching stderr avoids the jobs status change output. - _jrun "$@" 2>/dev/null + # This justs lets the journal output its last line + # before the prompt comes up. + sleep .5 + kill $jr_pid &>/dev/null ||: + unset jr_pid + fg &>/dev/null ||: } sm() { -- 2.30.2