Browse Source

added -B and -D options, with description

\/\/ \-/ ❭❬ 5 years ago
parent
commit
0db065963f
1 changed files with 22 additions and 3 deletions
  1. 22
    3
      mhssh

+ 22
- 3
mhssh View File

@@ -23,10 +23,13 @@ red=$(tput setaf 1)
23 23
 grn=$(tput setaf 2)
24 24
 raz=$(tput sgr0)
25 25
 
26
+bgnd=''
27
+dtch=''
28
+
26 29
 usage() {
27 30
 	echo "Run a single command on multiple hosts"
28
-	echo "Usage : $0 [-h|--help] [-n|--no-color] CMD ..."
29
-	echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n"
31
+	echo "Usage : $0 [-h|--help] [-n|--no-color] [-B|--background] [-D|--detach] CMD ..."
32
+	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"
30 33
 	echo "Host list configured to be found in '$hosts'"
31 34
 }
32 35
 
@@ -50,6 +53,14 @@ case "$1" in
50 53
 		grn=''
51 54
 		raz=''
52 55
 	;;
56
+	-B|--background)
57
+		shift
58
+		bgnd='on'
59
+	;;
60
+	-D|--detach)
61
+		shift
62
+		dtch='on'
63
+	;;
53 64
 esac
54 65
 
55 66
 if [ ! -f "$hosts" ]
@@ -67,6 +78,14 @@ do
67 78
 	stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")
68 79
 	stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz")
69 80
 	{
70
-		ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2
81
+		if [ -n "$bgnd" ];
82
+		then
83
+			ssh $host "nohup $@ > /dev/null < /dev/null & PID=\$!; disown \$PID; echo \$PID; exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2;
84
+		elif [ -n "$dtch" ];
85
+		then
86
+			ssh $host "TMPFILE=$(mktemp -u); dtach -n \$TMPFILE $@; echo \$TMPFILE; exit" 2>&3 | sed -e "s/^/dtach session : $stdout/" 1>&2 ;
87
+		else
88
+			ssh $host "$@;exit" 2>&3 | sed -e "s/^/$stdout/" 1>&2 ;
89
+		fi
71 90
 	} <&0 3>&1 | sed -e "s/^/$stderr/"
72 91
 done

Loading…
Cancel
Save