-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmux
executable file
·62 lines (55 loc) · 2.08 KB
/
stmux
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
#!/bin/sh
# tmux shared session wrapper script, by Lucas Ritzdorf (v3.1)
# Manages socket stuff, to make session sharing easier
# To allow session sharing based on a group other than users' primary group,
# ensure that users' shell sessions "export STMUX_GROUP=whatever". Their
# sessions will then be joinable by anyone else in that group.
# Configuration variables
# You can change these!
stmux_dir=/tmp/tmux-shared
# Check arguments
username=$(whoami)
if [ -z "$1" ] ; then
socket=$username
else
socket=$1
fi
session=$socket
if [ -n "$2" ] ; then
echo "Extra arguments detected; ignoring..."
fi
# Create a shared folder for tmux sessions, if it doesn't exist already
if ! [ -d $stmux_dir ] ; then
mkdir $stmux_dir
chown "${STMUX_GROUP:-}" $stmux_dir
chmod g+ws $stmux_dir
fi
# Start tmux using a shared socket
if [ -S $stmux_dir/$socket ] ; then
# The socket already exists...
if tmux -S $stmux_dir/$socket has-session -t $session ; then
# ...and has the desired session...
if [ $session = $username ] ; then
# ...and is our session; connect to it
echo "Existing session found, connecting directly..."
tmux -S $stmux_dir/$socket attach -t $session
else
# ...and is not already our session; link to the existing session with a new one
echo "Creating new session to track existing target session..."
tmux -S $stmux_dir/$socket new-session -d -t $session -s $username \; set-option -q -t $username destroy-unattached
fi
else
# ...but does not contain the target session; create it
echo "Creating new session in existing socket..."
tmux -S $stmux_dir/$socket new-session -s $session
fi
else
# The socket does not yet exist; create it and start a session
echo "Creating socket..."
tmux -S $stmux_dir/$socket new-session -s $session
fi
# After tmux exits, check if socket is still in use...
if [ -e $stmux_dir/$socket ] && ! tmux -S $tmux_dir/$socket ls &> /dev/null ; then
# ...and if not, delete it
rm $stmux_dir/$socket &> /dev/null
fi