fix jrun
[distro-setup] / brc2
diff --git a/brc2 b/brc2
index 50d51baab5ff3b3b03c98932a5bc0068a4c8235a..d9c7cbde71d5da4126b1466c106e699e9375d02b 100644 (file)
--- 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() {