2

I'd love to be able to write a script that checked for the presence of a tmux session and attach it, or if no session exists, start a new one. So that I could create a custom command in my .zshrc file, and not have to always check.

Is there a way to check programmatically that I have a tmux screen session running?

U880D
  • 597
  • 7
  • 17
TheDelChop
  • 123
  • 1
  • 5

2 Answers2

4

There is an answer over on StackOverflow https://stackoverflow.com/questions/3432536/tmux-create-session-if-none-exists

tmux attach || tmux new
slillibri
  • 1,633
  • 1
  • 9
  • 8
1

tmux ls will list running sessions

If you're using this inside a .shrc file or similar with exec I'd recommend

if tmux ls
  exec tmux attach
else
  exec tmux
fi
Aaron J Lang
  • 111
  • 2