如何在 (gnu-)screen 中快速查看分离的会话?

如何在 (gnu-)screen 中快速查看分离的会话?

假设我在(gnu-)screen 中有一个独立的会话:

$ screen -ls
There are screens on:
    3629.pts-0.confusion    (03/11/22 15:50:21) (Detached)
1 Sockets in /run/screen/S-root.

是否有一个好的方法,只用一个命令就可以快速查看该会话中发生的情况并立即再次分离?

我不想执行整个过程screen -r 3629.pts-0.confusion,然后按Ctrl+ a, d。而是只需执行如下操作:

$ screen <some option> 3629.pts-0.confusion | tail -n 2
confus@confusion:/mnt/d2# rsync -aHXAh --info=progress2 /some/dir /some/other/dir
         19.30G  10%    5.64MB/s    7:34:31  xfr#0, to-chk=66/321252)
$ # ... coninue about by usual business outside of screen ...

看看现在发生了什么?如果你有几个会话并且可以检查它们,那就太好了for x in session{1..10}; do screen <some option> $x | tail -n 5; done

答案1

回答

是的,您有几个选择:

  1. 将硬拷贝命令发送到要检查的屏幕并跟踪生成的文件
  2. 打开您感兴趣的窗口的日志记录并跟踪日志文件(除非您实际上经常检查它们,例如每隔一两分钟,否则我不会这样做)。

选项 1 的 TL;DR:

# In this example the third window/tab (number 2) has the title "test_title", and 
# "test_session" is the session name.

# Set the hardcopy output directory to /tmp (only needs to happen once per screen session):
screen -S test_session -p 2 -X hardcopydir /tmp

# Output the last two lines of the third window (number 2):
screen -S test_session -p 2 -X hardcopy && tail -n2 /tmp/hardcopy.2 && rm /tmp/hardcopy.2

解释和更多选项

我将详细介绍选项 1,因为这很可能是您想要采取的路线。

  1. 您可能会想要更改屏幕会话的默认屏幕目录,以便硬拷贝(如果您决定采用这种方式,那么还包括日志)可以到达您期望的位置。
    命令:

    # In this example the third window/tab (number 2) has the title "test_title", 
    # and "test_session" is the session name.
    #
    # Either of these commands will work, and you don't need to pass 
    # "-S test_session" if you only have one screen session and/or you don't
    # need to pass "-p title/number" if you only have one window in that session.
    
    screen -S test_session -p test_title -X chdir /tmp
    screen -S test_session -p 2 -X chdir /tmp
    
    # If only 1 window in the session:
    screen -S test_session -X chdir /tmp
    # If only 1 session (with multiple windows):
    screen -p 2 -X chdir /tmp
    
    # Can also only change the directory for hardcopy, if preferred:
    screen -S test_session -p 2 -X hardcopydir /tmp
    

    相关屏幕人条目:

    目录[目录]

    将 screen 的当前目录更改为指定目录,或者,如果调用时不带参数,则更改为您的主目录(环境变量 $HOME 的值)。通过“.screenrc”中的“screen”命令或通过“Ca : screen ...”或“Ca c”创建的所有窗口都使用此目录作为其默认目录。如果没有 chdir 命令,则这将是调用 screen 的目录。硬拷贝和日志文件始终写入窗口的默认目录,而不是窗口中运行的进程的当前目录。您可以在 .screenrc 中多次使用此命令以在不同的默认目录中启动各种窗口,但最后一个 chdir 值将影响您以交互方式创建的所有窗口。

    硬拷贝目录 目录

    定义放置硬拷贝文件的目录。如果未设置,硬拷贝将转储到屏幕的当前工作目录中。

  2. 发送 hardcopy 命令以使用窗口的当前输出创建一个文件:
    命令:

    # In this example the third window/tab (number 2) has the title "test_title", 
    # and "test_session" is the session name.
    #
    # Same rules as above apply if you only have one session or only one window
    # in a session, so I'll only use the example where you provide the session
    # name and window number here.
    
    # Create a hardcopy with the default filename:
    screen -S test_session -p 2 -X hardcopy
    
    # Create a hardcopy with a designated filename (in this case "output.tmp"):
    # NOTE: in my testing, this will put the file in the default directory defined
    #       by "chdir" and will ignore the hardcopy directory defined by "hardcopydir"
    screen -S test_session -p 2 -X hardcopy output.tmp
    
    # Create a hardcopy with the default filename including more lines
    # than are currently shown in the window:
    screen -S test_session -p 2 -X hardcopy -h
    

    相关屏幕人条目:

    硬拷贝[-H][文件]

    将当前显示的图像写入文件文件,或者,如果没有指定文件名,则 硬拷贝 在默认目录中,n是当前窗口的编号。这将追加或覆盖文件(如果存在)。见下文。如果选项-H被指定时,还转储回滚缓冲区的内容。

  3. 跟踪文件。这个命令很容易理解,但我将列出命令,以便您看到所有内容协同工作:
    命令:

    # In this example the third window/tab (number 2) has the title "test_title", 
    # and "test_session" is the session name. It also assumes that you
    # changed screen's default directory or hardcopy directory to /tmp.
    #
    # Same rules as above apply if you only have one session or only one window
    # in a session, so I'll only use the example where you provide the session
    # name and window number here.
    
    # See the last 2 lines of window number 2 in session "test_session":
    screen -S test_session -p 2 -X hardcopy test.output && tail -n2 /tmp/test.output
    
    # Same as above but delete the file afterward (just in case it has sensitive 
    # information or something):
    screen -S test_session -p 2 -X hardcopy test.output && tail -n2 /tmp/test.output && rm /tmp/test.output
    

其他可能相关的屏幕人条目

硬拷贝追加 |离开

如果设置为“on”,screen 将附加到由命令“Ca h”创建的“hardcopy.n”文件,否则每次都会覆盖这些文件。默认为“off”。

参考

  • screentail手册页

相关内容