Linux 通过创建新的屏幕窗口附加到“屏幕”会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10599769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:19:57  来源:igfitidea点击:

Attach to 'screen' session with creating a new screen window

linuxshellgnu-screendetach

提问by Alfe

I have a screen session running with several windows. I want to attach to it, create a new screen window within it and start a shell in that new window.

我有一个运行多个窗口的屏幕会话。我想附加到它,在其中创建一个新的屏幕窗口并在该新窗口中启动一个 shell。

Question: How can I do this from the command line outside the screen session?

问题:如何从 screen 会话外的命令行执行此操作?

I already tried a lot, e. g. screen -x(but it attaches to one of the existing screen windows; it does not create a new one nor does it start a new shell). Any hints are welcome.

我已经尝试了很多,例如screen -x(但它附加到现有的屏幕窗口之一;它不会创建一个新的窗口,也不会启动一个新的外壳)。欢迎任何提示。

Environment is Linux (Ubuntu 12.04).

环境是 Linux (Ubuntu 12.04)。

采纳答案by zondo

To choose a window to join, use the -poption. Giving +will create a new one, so your command is simply:

要选择要加入的窗口,请使用该-p选项。给予+将创建一个新的,所以你的命令很简单:

screen -x session_name -p +

This is documented on the man page:

这记录在手册页上:

-p n̲u̲m̲b̲e̲r̲_o̲r̲_n̲a̲m̲e̲|̲-̲|̲=̲|̲+̲

Preselect a window. This is useful when you want to reattach to a  
specific windor or you want to send a command via the "-X" option  
to a specific window. As with screen's select commant, "-" selects  
the blank window. As a special case for reattach, "=" brings up the  
windowlist on the blank window. 

-p n̲u̲m̲b̲e̲r̲_o̲r̲_n̲a̲m̲e̲|̲-̲|̲=̲|̲+̲

Preselect a window. This is useful when you want to reattach to a  
specific windor or you want to send a command via the "-X" option  
to a specific window. As with screen's select commant, "-" selects  
the blank window. As a special case for reattach, "=" brings up the  
windowlist on the blank window. 

回答by Alfe

I found something on the mailing list, thank you tuxuday :)

我在邮件列表上找到了一些东西,谢谢 tuxuday :)

I'm doing it now this way:

我现在这样做:

#!/bin/bash
screen -X screen -t NEWWINDOW  # create new window (switches existing attached terminal)
sleep 0.1
screen -X other  # switch existing attached terminal back to its old window
sleep 0.1
gnome-terminal -e 'screen -x -p NEWWINDOW'  # connect to new window

I'm not sure about those sleeps, maybe they aren't necessary in all environments, but I can wait those 0.2s easily.

我不确定这些睡眠,也许它们在所有环境中都不是必需的,但我可以轻松地等待 0.2 秒。

My .bash_aliasesis changing the screen window title with a delay, so the awkward NEWWINDOWwon't stick for long (and thus not hinder further calls to this script).

.bash_aliases正在延迟更改屏幕窗口标题,因此尴尬NEWWINDOW不会持续很长时间(因此不会妨碍对该脚本的进一步调用)。

回答by Yuriy Tumakha

Add new detached window to sesion_nameand run command

添加新的分离窗口sesion_name并运行command

screen -S sesion_name -x -X screen bash -c 'command; exec bash'