-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmux-start.sh
executable file
·258 lines (221 loc) · 5.14 KB
/
tmux-start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env bash
# $Id: tmux-start.sh,v 1.16 2015/11/01 22:16:34 swilson Exp $
# Author: Shawn Wilson <[email protected]>
# Copyright 2015 Korelogic Inc. https://www.korelogic.com
# GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
# http://www.gnu.org/licenses/gpl-3.0.html
# basename
script="${0##*/}"
IFS="." read -a bash_version <<<"${BASH_VERSION}"
die()
{
echo -ne "$@" >&2
exit 1
}
warn()
{
echo "WARN: $@\n" >&2
}
debug()
{
if [ -n "$DEBUG" ] ; then
warn "$@"
fi
}
opthelp()
{
die "$@\n" \
"$script [OPTIONS]\n" \
" -a string to append to the command for each variable\n" \
" -c command to run\n" \
" -f force (restarts session with the same name)\n" \
" -n name of the array to use\n" \
" -p prefix to insert into the tmux session name\n" \
" -P Pause between commands\n" \
" -r Max retries per command\n" \
" -s sourced config file with array of variables (warning: " \
"contains code)\n" \
" -S interactive shell (eg: bash --norc --noprofile)\n" \
" -x run each element as a full command\n" \
" -d debuging\n" \
" -h this message\n"
}
session_test ()
{
local session="$@"
tmux list-window -t "$session" 2>/dev/null | wc -l
}
# Will be exported to every session that is started
_rerun_cmd ()
{
local cmd="$1"
if [[ -z "$cmd" ]] ; then
return
fi
local max="$2"
# Max needs to be defined just in case the command will never succeed
if [[ -z "$max" ]] ; then
max=10
fi
local sleep="$3"
if [[ -z "$sleep" ]] ; then
sleep=1
fi
local count=0
while eval $cmd ; do
if ((count >= max)) ; then
break
fi
sleep "$sleep";
done
}
config=~/.config/host-vars.sh
# Catch no options
if [[ "$#" -eq 0 ]] ; then
opthelp
fi
# Default values
pause=0
while getopts ":a:n:c:fP:p:r:S:s:xdh" opt; do
case "$opt" in
# Append string
a)
append="${OPTARG}"
;;
# Command
c)
cmd="${OPTARG}"
;;
# Force a kill and respawn of a session with the same name
f)
force=1
;;
# Variable name
n)
name="${OPTARG}"
;;
# Pause value
P)
pause="${OPTARG}"
;;
# Tmux session name prefix
p)
prefix="${OPTARG}"
;;
# Max retries
r)
retries="${OPTARG}"
;;
# Shell
S)
shell="${OPTARG}"
;;
# Config file
s)
[[ -e "${OPTARG}" ]] || die "Config file [${OPTARG}] not found."
config="${OPTARG}"
;;
# No command eval
x)
nocmd=1
;;
# Debug
d)
debug=1
;;
# Help
h)
opthelp
;;
*)
opthelp
;;
esac
done
shift $((OPTIND-1))
# Make sure we know if nothing is specified to be run
if [[ -z "$cmd" && -z "$nocmd" ]] ; then
die "The -x option must be specified if not giving a command.\n"
fi
# Check proper command
if ! ( [[ -x "${cmd%% *}" ]] || type "${cmd%% *}" >/dev/null 2>&1 ) ; then
die "Not a command [${cmd%% *}]\n"
else
read -r acmd aparams <<<"${cmd}"
fullcmd="$(type -p $acmd)"
if [[ -n "$fullcmd" ]] ; then
cmd="$fullcmd $aparams"
else
cmd="$acmd $aparams"
fi
fi
# Sanity checks
if [ "${bash_version[0]}" -lt 4 ] ; then
die "Bash version less than 4 is not supported\n"
fi
if ! type tmux >/dev/null 2>&1 ; then
die "tmux does not exist\n"
fi
source "$config" 2>/dev/null
unset var
declare -n var="$name"
if [[ -z "$var" ]] ; then
die "$name not defined in config [$config]\n"
else
debug "Elements: ${var[@]}"
fi
if [[ -z "$shell" ]] ; then
case "${SHELL##*/}" in
bash)
shell="bash --norc --noprofile"
;;
zsh)
shell="zsh -f -i"
;;
esac
fi
offset=0
for ((count = 0 ; count < 10 ; count++)) ; do
session="${prefix}${name}${count}"
# Increment if the session name is already in use
if [[ $(session_test "$session") -gt 0 ]] ; then
if [[ -n "$force" ]] ; then
tmux kill-session -t "$session"
else
continue
fi
fi
# Create a new session - temporarily unset TMUX so it doesn't complain
# if already inside tmux - -d makes this DWIM
TMUX= tmux -2 new-session -d -s "$session"
while [ $(session_test "$session") -eq 0 ] ; do
sleep 1
done
# Make sure we only start 10 windows per session so meta-[0-9] work for
# all windows
if ((offset + 10 > ${#var[@]})) ; then
max=$((${#var[@]} - $offset))
end=1
else
max=10
fi
for ((num = 0 ; num < "$max" ; num++)) ; do
# Window name
unit="${var[${offset} + ${num}]}${append}"
debug "Window name [$unit] Session [${session}:${num}]"
# Start off with a new window
if [[ "$num" -gt 0 ]] ; then
tmux new-window -t "${session}:${num}" -n "$unit" -- "$shell"
fi
# Declare a private function to give commands (like ssh that might get
# bogged down) a chance to start
tmux send-keys -t "${session}:${num}" " $(declare -f _rerun_cmd)" C-m
tmux send-keys -t "${session}:${num}" " clear" C-m
tmux send-keys -t "${session}:${num}" "_rerun_cmd \"${cmd} ${unit}\" \"$retries\" \"$pause\"" C-m
done
# end when the array "var" is done
if [[ -n "$end" ]] ; then
break
fi
((offset+=10))
done