WIP background tasks #2

Open
maxime-alves wants to merge 1 commit from (deleted):bg_tasks into master

25
mhssh
View file

@ -23,10 +23,13 @@ red=$(tput setaf 1)
grn=$(tput setaf 2)
raz=$(tput sgr0)
bgnd=''
dtch=''
usage() {
echo "Run a single command on multiple hosts"
echo "Usage : $0 [-h|--help] [-n|--no-color] CMD ..."
echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n"
echo "Usage : $0 [-h|--help] [-n|--no-color] [-B|--background] [-D|--detach] CMD ..."
echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\t-B --background\tput the task in background\n\t-D --detach\tuse dtach to put the task in background\n\n"
echo "Host list configured to be found in '$hosts'"
}
@ -50,6 +53,14 @@ case "$1" in
grn=''
raz=''
;;
-B|--background)
shift
bgnd='on'
;;
-D|--detach)
shift
dtch='on'
;;
esac
if [ ! -f "$hosts" ]
@ -67,6 +78,14 @@ do
stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")
stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz")
{
ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2
if [ -n "$bgnd" ];
then
ssh $host "nohup $@ > /dev/null < /dev/null & PID=\$!; disown \$PID; echo \$PID; exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2;
elif [ -n "$dtch" ];
then
ssh $host "TMPFILE=$(mktemp -u); dtach -n \$TMPFILE $@; echo \$TMPFILE; exit" 2>&3 | sed -e "s/^/dtach session : $stdout/" 1>&2 ;
else
ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2 ;
fi
} <&0 3>&1 | sed -e "s/^/$stderr/"
done