Bash, server exited unexpectedly after killing tmux process

Viewed 415

I made a script in bash to load a custom tmux session.

I noticed that because the windows has a name, I can't use again the script, because the windows name already exists. Instead of using some random number as name for the windows, or don't give it a name at all, I thought it'd easier to kill the process.

#!/bin/sh

pkill -f tmux

tmux new-session -s "Tab 1"\; \
  split-window -v \; \
  split-window -h \;  \
  split-window -v \;  

So, when I use the script it kills the tmux process before creating the tmux session. I noticed that if the tmux session is open, it closes it but another does not open. It throws me this message instead.

server exited unexpectedly

So I have to use the command twice to make it works.

Is there any way of executing the script one single time killing the current session and opening a new one? Thanks!

1 Answers

I don't know if this is the best way to go about it, but from looking around in man tmux I found the tmux detach-client command:

Detach the current client if bound to a key, the client specified with -t, or all clients currently attached to the session specified by -s. The -a option kills all but the client given with -t. If -P is given, send SIGHUP to the parent process of the client, typically causing it to exit. With -E, run shell-command to replace the client.

Useful here is the -E option. We could use it to kill the current session and start a new session with the options you want.

A one-liner solution (If you're inside tmux):

tmux detach-client -E "tmux kill-session; tmux new-session -s 'Tab 1'\; split-window -v\; \; split-window -v \; split-window -h"
Related