【问题标题】:Send command to a detached screen, which changes the active window将命令发送到分离的屏幕,这会更改活动窗口
【发布时间】:2015-06-18 21:56:36
【问题描述】:

如何向分离的 GNU Screen 会话发送命令,从而更改当前活动屏幕?因此,如果我在第三个窗口中离开会话,在发出命令并再次打开屏幕会话后,我可能会在第四个窗口中。 我试过类似的东西:

screen -r -X 'screen next'

但它没有奏效。 “next”是切换到下一个窗口的屏幕命令。

我想做的是: 我有一个由 Raspberry Pi 和 USB 硬盘制成的超便宜的自制种子盒。由于它只有 256Mb RAM,我无法同时运行所有种子(大约 2000 个)。

那我该怎么办。将种子分成 6 个文件夹,在每个文件夹中运行带有 rtotrrent 实例的屏幕。每天我都在正在运行的文件夹中停止 rtorrrent,并在下一个文件夹中启动它。

我正在尝试使用 cron 为我制作一个 shell 脚本,但我无法解决这个问题。如何用下一个改变当前屏幕窗口..

我想在屏幕上运行它,因为我希望能够看到我现在正在播种的内容。

谢谢

【问题讨论】:

    标签: linux shell gnu-screen


    【解决方案1】:

    切换到下一个窗口的正确命令

    screen -r -X 'next'
    

    【讨论】:

      【解决方案2】:

      至少在我的屏幕版本¹上,通过-X 向分离的会话发送命令不适用于可能更改当前窗口的内容(例如nextselect 0)。其他命令,例如 stuff,确实有效。也许这是一个错误,我不知道。我怀疑这是“设计使然”,因为 -Q select ... 标志也有同样的问题,如果可以的话,我希望它能解决这个确切的问题。

      我对您提出的问题有进一步的解决方案,但我认为您可以像这样解决您的实际问题:

      screen -S mysession -p 0 -X stuff 'some commands for your torrents'
      

      在这里,我们不更改活动窗口,但我们使用 -p 选项将命令发送到特定窗口(在本例中为 0)。

      另一个更厚脸皮的回答是“使用tmux——它对脚本有更好的支持”(:

      ¹ 版本 4.06.02 (GNU) 23-Oct-17


      希望能解决您的问题,但我来到这里是因为我确实需要使用脚本更改屏幕会话中的活动窗口。

      而且,在尝试解决您的问题时,我找到了我的解决方案,所以我会在此处添加,以防其他人感兴趣。

      请注意,如果您通过某个终端手动连接,那么您可以在脚本中通过-X 发送nextselect 命令就可以了。但当然,我们不想手动附加;我们想自动化它。

      所以...为可憎的事情做好准备(:

      我们将使用单独的屏幕会话附加到我们关心的屏幕会话,从而将该会话置于“附加”状态,我们可以从主脚本向其发送select 命令。我们将从另一个脚本屏幕会话中编写屏幕脚本。

      假设我们有一个名为foo 的现有会话,我们想向它发送select 0 命令。只运行这个不起作用:screen -S foo -X select 0

      相反,请执行以下操作:

      #!/usr/bin/env bash
      # (the only bash-ism here is $'\n', so you could do it with
      # plain 'sh' easily, too)
      
      # You could get this from a commandline argument, etc.
      target_session=foo
      
      # A unique name for our controller session. You could append a UUID
      # if you might have two scripts running, but they would fight with
      # each other anyway - probably best to avoid.
      controller_session="control_$target_session"
      
      # Create the controller session.
      # It starts in detached state, so we can script against it.
      screen -d -m -S "$controller_session" -t attacher
      
      # The default window will be used for attaching to $target_session.
      # We make a second window for running our "real" commands.
      # We could also do this in a separate process or somesuch.
      screen -S "$controller_session" -X screen -t runner
      
      # Attach to the target session
      # Note: if someone else was attached (eg: if you were watching), it
      # will fail. But in that case, our subsequent command still have an
      # attached session to work with, so our overall script will work.
      screen -S "$controller_session" -p 0 -X stuff "screen -r '$target_session'"$'\n'
      
      # Now that it's attached, we can run commands which change the active
      # window.  This is the real meat of this script, where we do the thing
      # we care about.
      screen -S "$controller_session" -p 1 -X stuff "screen -S '$target_session' -X select 0"$'\n'
      
      # Hack!
      # At least on my system, there needs to be some delay between the select
      # command being run, and detaching. Otherwise, it does not take effect.
      # 0.01 is too small, 0.05 seems to work, so this should be plenty.
      sleep 0.1
      
      # Detach from target_session
      screen -S "$target_session" -X detach
      # Kill the controller, since we're done with it.
      screen -S "$controller_session" -X quit
      

      它不漂亮,但我知道我会用它!

      【讨论】:

        猜你喜欢
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        • 2017-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多