如何在 Gnu-screen 中打开选项卡窗口并在每个窗口中执行命令

Ahm*_*ein 36 gnu-screen

当启动会话命名为这样的任何名称时 screen -S name1

我想在这个屏幕会话中打开标签窗口,就像在 gnome-terminal 中打开标签一样

gnome-terminal --tab -e "some commands"
Run Code Online (Sandbox Code Playgroud)

那么怎么做呢?

slm*_*slm 81

1. 屏幕标签

您正在寻找将其添加到您的 .screenrc 文件中:

screen -t tab1
screen -t tab2
Run Code Online (Sandbox Code Playgroud)

Here's a nice basic .screenrc to get you started with a status bar etc. NOTE: This is typically located in your home directory /home/<username>/.screenrc.

screen -t validate #rtorrent
screen -t compile #irssi
screen -t bash3
screen -t bash4
screen -t bash5
altscreen on
term screen-256color
bind ',' prev
bind '.' next
#
#change the hardstatus settings to give an window list at the bottom of the
#screen, with the time and date and with the current window highlighted
hardstatus alwayslastline
#hardstatus string '%{= kG}%-Lw%{= kW}%50> %n%f* %t%{= kG}%+Lw%< %{= kG}%-=%c:%s%{-}'
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
Run Code Online (Sandbox Code Playgroud)

screenshot

屏幕会话的 ss

2. Tabs in screen (with commands run inside)

The example .screenrc below will create 2 tabs and run 3 echo commands in each.

screen -t tab1                                                                                     
select 0                                                                                           
stuff "echo 'tab1 cmd1'; echo 'tab1 cmd2'; echo 'tab1 cmd3'^M"                                     
screen -t tab2                                                                                     
select 1                                                                                           
stuff "echo 'tab2 cmd1'; echo 'tab2 cmd2'; echo 'tab2 cmd3'^M"                                     

altscreen on                                                                                       
term screen-256color                                                                               
bind ',' prev                                                                                      
bind '.' next                                                                                      
#                                                                                                  
#change the hardstatus settings to give an window list at the bottom of the                        
#screen, with the time and date and with the current window highlighted                            
hardstatus alwayslastline                                                                          
#hardstatus string '%{= kG}%-Lw%{= kW}%50> %n%f* %t%{= kG}%+Lw%< %{= kG}%-=%c:%s%{-}'              
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
Run Code Online (Sandbox Code Playgroud)

This technique makes use of screen's select and stuff commands to initially select one of the tabs, and then "stuff" a string into it.

screenshot

带有选项卡和命令的屏幕 ss

3. Creating #2 without using a .screenrc file

If you're looking for the scenario where you can:

  1. create a screen session
  2. load it up with tabs
  3. have each tab running their own commands
  4. not require a .screenrc file

Then this is the one for you! Be prepared though. This one can get a little tricky with the command lines.

For starters let's create a screen session:

$ screen -AdmS myshell -t tab0 bash
Run Code Online (Sandbox Code Playgroud)

The switches -AdmS do the following:

(See the screen man page for more details)

-A

    Adapt the sizes of all windows to the size of the  current terminal. 
    By default, screen tries to restore its old window sizes when
    attaching to resizable terminals
Run Code Online (Sandbox Code Playgroud)

-d -m

    Start screen in "detached" mode. This creates a new session but
    doesn't attach to it. This is useful for system startup scripts.
Run Code Online (Sandbox Code Playgroud)

-S sessionname

    When creating a new session, this option can be used to specify a
    meaningful name for the session. This name identifies the session for
    "screen -list" and "screen -r" actions. It substitutes the default
    [tty.host] suffix.
Run Code Online (Sandbox Code Playgroud)

Now let's start loading it up with tabs + their commands:

$ screen -S myshell -X screen -t tab1 vim
$ screen -S myshell -X screen -t tab2 ping www.google.com
$ screen -S myshell -X screen -t tab3 bash
Run Code Online (Sandbox Code Playgroud)

These 3 commands will create 3 additional tabs and run vim, ping google, and launch a bash shell. If we list out the screen sessions we'll see the following:

$ screen -ls
There is a screen on:
        26642.myshell   (Detached)
1 Socket in /var/run/screen/S-root.
Run Code Online (Sandbox Code Playgroud)

If we connect to the screen session, myshell, and list the tabs that it contains we'll see the following:

$ screen -r myshell
Run Code Online (Sandbox Code Playgroud)

Hit this key combination: Ctrl+A followed by Shift+"

Num Name                                                                   Flags

  0 tab0                                                                       $
  1 tab1                                                                       $
  2 tab2                                                                       $
  3 tab3                                                                       $
Run Code Online (Sandbox Code Playgroud)

Switching to tab2:

64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_seq=443 ttl=55 time=41.4 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_seq=444 ttl=55 time=33.0 ms
64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_seq=445 ttl=55 time=30.1 ms
Run Code Online (Sandbox Code Playgroud)

screenshot

从 cli 启动的屏幕 ss

上面的命令是完成 OP 所寻找的基本方法。这当然可以使用 Bash 别名甚至 shell 脚本来压缩和细化,这只是为了展示能力和展示方式!

参考

  • 如果我没有错过阅读 Q,他想在会话开始时执行命令。通常`screen "cd /foo/bar/; 召唤_cthulhu;" &lt;&lt;-- 在那个屏幕会话中` (2认同)