updates, especially for etiona
[distro-setup] / brc2
diff --git a/brc2 b/brc2
index 891e0953b668e7e69d847f54e5f4ad24f768fff5..cdac34b690b40401c314c0138a543e7918674d09 100644 (file)
--- a/brc2
+++ b/brc2
@@ -114,11 +114,11 @@ bbk() { # btrbk wrapper
   local active=true
   systemctl is-active btrbk.timer || active=false
   if $active; then
-    ser disable btrbk.timer
+    ser stop btrbk.timer
   fi
-  if systemctl is-active btrbk.service; then
-    $active && ser enable btrbk.timer
+  if [[ $(systemctl is-active btrbk.service ||:) != inactive ]]; then
     echo "cron btrbk is already running"
+    if $active; then ser start btrbk.timer; fi
     return 1
   fi
   # run latest
@@ -126,9 +126,9 @@ bbk() { # btrbk wrapper
   jrun -p btrbk btrbk-run "$@"
   if $active; then
     if (( $ret )); then
-      echo bbk: WARNING: btrbk.timer not reenabled due to failure
+      echo bbk: WARNING: btrbk.timer not restarted due to failure
     else
-      ser enable btrbk.timer
+      ser start btrbk.timer
     fi
   fi
   return $ret
@@ -140,59 +140,60 @@ bigclock() {
   xclock -digital -update 1 -face 'arial black-80:bold'
 }
 
-_jrun() { # journal run. run args, log to journal, tail and grep the journal.
-  local pid pattern jr_pid sedscript cmd_name ended
+inttrap() {
+  pid=$1
+  # just passing on -INT doesnt work.
+  kill -TERM $pid
+  sleep .05
+  if [[ ! -e /proc/$pid ]]; then
+    if [[ $old_int_trap ]]; then
+      $old_int_trap
+    else
+      trap INT
+    fi
+  fi
+}
+
+
+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)
-      pattern="$2\|"
+      pattern="$2|"
       shift 2
       ;;
   esac
   cmd_name=${1##*/}
-  systemd-cat -t "$cmd_name" "$@" 2>&1 &
-  pid=$!
-  sedscript="/$pattern$cmd_name/p;/^.{16}[^ ]+ $cmd_name\[$pid]: ([^ ]+ +)?exiting with status [0-9]+\$/q"
-  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
-  jobs %'journalctl -S "4 seconds ago" -f' &>/dev/null
-  ended=false
-  # give it about 5 seconds to find the end of the log
-  for (( i=0; i<20; i++ )); do
-    if jobs %'journalctl -S "4 seconds ago" -f' &>/dev/null; then
-      sleep .3 2>&1
-    else
-      ended=true
-      break
-    fi
-  done
-  if ! $ended; then
-    kill $jr_pid 2>&1
+    echo "jrun: ERROR: $* returned $ret"
   fi
-}
-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 )) || return 1
-  action=$1
-  shift
-  host=$1
-  shift
-  case $action in
-    pull|push) : ;;
-    *) return 1 ;;
-  esac
   c /
   # run latest
   install-my-scripts
-  jrun -p btrbk switch-mail-host $action $host
+  jrun -p btrbk switch-mail-host "$@"
   return $ret
 }
 
@@ -355,6 +356,8 @@ envload() { # load environment from a previous: export > file
   done < "$file"
 }
 
+failfunc() { asdf a b c; }
+failfunc2() { failfunc d e f; }
 
 # one that comes with distros is too old for newer devices
 fastboot() {
@@ -804,18 +807,29 @@ resolvcat() {
   m s nscd -i hosts
   f=/etc/resolv.conf
   echo $f:;  ccat $f
-  hr; m ser status dnsmasq | cat
   hr; s ss -lpn 'sport = 53'
-  #hr; echo dnsmasq is $(systemctl is-active dnsmasq)
-  f=/var/run/dnsmasq/resolv.conf
-  hr; echo $f:;  ccat $f
-  hr; m grr '^ *servers-file *=' /etc/dnsmasq.conf /etc/dnsmasq.d
-  f=/etc/dnsmasq-servers.conf
-  hr; echo $f:;  ccat $f
+  if systemctl is-enabled dnsmasq &>/dev/null || [[ $(systemctl is-active dnsmasq ||:) != inactive ]]; then
+    # this will fail is dnsmasq is failed
+    hr; m ser status dnsmasq | cat || :
+    f=/var/run/dnsmasq/resolv.conf
+    hr; echo $f:;  ccat $f
+    hr; m grr '^ *(servers-file|server) *=|^ *no-resolv *$' /etc/dnsmasq.conf /etc/dnsmasq.d
+    f=/etc/dnsmasq-servers.conf
+    hr; echo $f:;  ccat $f
+  fi
+  if systemctl is-enabled systemd-resolved &>/dev/null || [[ $(systemctl is-active systemd-resolved ||:) != inactive ]]; then
+    hr; m ser status systemd-resolved | cat || :
+    hr; m systemd-resolve --status
+  fi
+
 }
 rcat() {
   resolvcat | less
 }
+reresolv() {
+  sudo nscd -i hosts
+  #sudo systemctl restart dnsmasq
+}
 
 # only run on MAIL_HOST. simpler to keep this on one system.
 r2eadd() { # usage: name url
@@ -1216,6 +1230,8 @@ wtr() { curl wttr.in/boston; }
 
 xevkb() { xev -event keyboard; }
 
+ziva() { e "toot! i love dancing. fart"; }
+
 # * misc stuff
 
 # from curl cheat.sh/:bash_completion