X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=brc2;h=61bd93109a60b49b3e78c64b06f0bdbf680ae124;hb=d6def754cd241538c61456536b52ee51cbd85b42;hp=ea1faafd479eb2b1f5128728f71c41a5853fd991;hpb=fa264ab2ec907b4ebc8d7dd0f8b239b0d0202362;p=distro-setup diff --git a/brc2 b/brc2 index ea1faaf..61bd931 100644 --- a/brc2 +++ b/brc2 @@ -364,7 +364,7 @@ ap() { } aw() { pushd /a/work/ans >/dev/null - time ansible-playbook -v -i inventory adhoc.yml "$@" + time ansible-playbook -i inventory adhoc.yml "$@" popd >/dev/null } ad() { @@ -414,7 +414,7 @@ bbk() { # btrbk wrapper install-my-scripts # todo: consider changing this to srun and having the args come # from a file like /etc/default/btrbk, like is done in exim - s jrun btrbk-run "$@" + s jdo btrbk-run "$@" if $active; then if (( ret )); then echo bbk: WARNING: btrbk.timer not restarted due to failure @@ -454,40 +454,90 @@ locat() { # log-once cat ngreset } -# duplicated somewhat below. -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 using systemd-cat, but this seems obviously better, - # and that seemed to have a problem exiting during a systemctl daemon-reload - local cmd_name jr_pid s +scr() { + screen -RD "$@" +} + + +# version of jdo for my non-root user +jdo() { + # comparison of alternative logging methods: + # + # systemd-run command (what this function does) + # + # If there is a user prompt, the program will detect that it is not + # connected to a terminal and act in a non-interactive way, skipping + # the prompt. This has the benefit that you know exactly how the + # program will act if you want to move it into a service that runs + # automatically. + # + # If run with sudo and command is a shell script which does a sleep, + # it can (sometimes?) output some extra whitespace in front of + # messages, more for each subsequent message. This can be avoided by + # becoming root first. + # + # It logs the command's pid and exit code, which is nice. + # + # + ### command |& ts | tee file.log + # + # If there is a user prompt, like "read -p prompt var", it will hang + # without outputting the prompt. + # + # I've had a few times where ts had an error and I wasn't totally sure + # if it was really the command or ts having the problem. + # + # Sometimes some output will get hidden until you hit enter. + # + # + ### command |& pee cat logger + # + # This seems to work. I need to test more. + # + # + ### command |& logger -s + # + # User prompts get confusingly prefixed to earlier output, and all log + # entries get prefixed with annoying priority level. + # + # + ### systemd-cat + # + # Had a few problems. One major one is that it exited in the middle of + # a command on systemctl daemon-reload + # + # Related commands which can log a whole session: script, sudo, screen + local cmd cmd_name jr_pid ret ret=0 - cmd_name=${1##*/} - cmd=$1 + cmd="$1" + shift + cmd_name=${cmd##*/} if [[ $cmd != /* ]]; then - cmd=$(which $1) + cmd=$(type -P "$cmd") fi + # -q = quiet journalctl -qn2 -f -u "$cmd_name" & - # Guess of time needed to avoid missing initial lines. + # Trial and error of time needed to avoid missing initial lines. # .5 was not reliable. 1 was not reliable. 2 was not reliable sleep 4 - # 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=$! # note, we could have a version that does system --user, but if for example # it does sudo ssh, that will leave a process around that we can't kill # and it will leave the unit hanging around in a failed state needing manual # killing of the process. - m s systemd-run --uid $(id -u) --gid $(id -g) \ + s systemd-run --uid $(id -u) --gid $(id -g) \ -E SSH_AUTH_SOCK=/run/openssh_agent \ - --unit "$cmd_name" --wait --collect "$cmd" "${@:2}" || ret=$? - # This justs lets the journal output its last line + --unit "$cmd_name" --wait --collect "$cmd" "$@" || ret=$? + # The sleep 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 ||: + # this avoids any err-catch + (( $ret == 0 )) || return $ret } + # service run, and watch the output srun() { local unit @@ -511,7 +561,7 @@ sm() { # switch mail host s ssh-add /root/.ssh/home fi install-my-scripts - s jrun switch-mail-host "$@" + s jdo switch-mail-host "$@" return $ret } sh2() { # switch host2 @@ -524,7 +574,7 @@ sh2() { # switch host2 s ssh-add /root/.ssh/home fi install-my-scripts - s jrun switch-host2 "$@" + s jdo switch-host2 "$@" return $ret } @@ -1435,6 +1485,35 @@ allmyirc() { ssh root@iankelling.org "cd $d; find . -mtime -60 -type f -exec grep '\