如果不存在,如何创建新的 tmux 会话 [英] How to create new tmux session if none exists

查看:26
本文介绍了如果不存在,如何创建新的 tmux 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果存在命名的 tmux 会话,我想弄清楚如何附加到 tmux 会话,如果不存在,我想创建一个具有给定名称的新会话.

I am trying to figure out how to attach to a tmux session if a named tmux session exists, if not I want to create a new one with the given name.

目前,我知道一些 tmux 命令可以部分实现我正在寻找的东西,但不清楚如何将它们组合在一起以获得我正在寻找的东西:

Currently, I know of a few tmux commands which can partly achieve what I am looking for, but its not clear how to combine them together to get what I am looking for:

  • tmux attach 附加到自动存在的会话 - 但如果不存在会话则出错
  • tmux new 创建一个新会话 - 但它每次都这样做,所以我不能把它留在我的 .tmux.conf
  • tmux has-session 测试会话是否存在 - 但我不知道如何将它与其他命令拼接在一起
  • tmux attach attaches to an automatically existing session - but errors out if no session exists
  • tmux new creates a new session - but it does so every time, so I can't leave it in my .tmux.conf
  • tmux has-session tests whether a session exists - but I don't know how to stitch it together with the other commands

因此,我想创建一个 tmux 脚本,以便自动执行此操作,而不必在每次需要登录会话时手动创建它.

Thus, I would like to create a tmux script, so that this happens automatically, instead of having to manually create it everytime I need to log into a sessions.

如何编写自动脚本以创建新的 tmux 会话(如果给定的会话名称不存在)或附加到会话名称(如果存在)?

How can I write a automatic script so as to create a new tmux session (if a given session name doesnt exist) or attach to a session name (if it exists)?

推荐答案

或者,您可以添加

new-session

到您的 .tmux.conf - 这将在服务器启动时创建一个默认会话.

to your .tmux.conf - that will create a default session on server start.

然后 tmux attach 要么附加到当前会话(正在运行的服务器,即),要么创建一个新会话(启动服务器,读取配置文件,发出 new-session 命令)并附加到该命令.

Then tmux attach will either attach to the current session (running server, that is), or create a new session (start the server, read the config file, issue the new-session command) and attach to that.

这篇关于如果不存在,如何创建新的 tmux 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆