将命令发送到分离屏幕,该屏幕将更改活动窗口

时间:2015-04-13 14:28:38

标签: linux shell gnu-screen

如何将命令发送到分离的GNU Screen会话,这会改变当前的活动屏幕?因此,如果我在第3窗口上离开会话,在给出命令并再次打开屏幕会话后,我可以在第4个窗口中。 我试过像:

screen -r -X 'screen next'

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

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

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

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

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

由于

2 个答案:

答案 0 :(得分:0)

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

screen -r -X 'next'

答案 1 :(得分:0)

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

我可以进一步解决您所陈述的问题,但是我认为您可以像这样解决您的 actual 问题:

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

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

另一个更为厚脸皮的答案是“使用tmux-它对脚本编写有更好的支持”(:

¹版本4.06.02(GNU)17年10月23日


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

而且,在尝试解决您的问题时,我找到了我的解决方案,因此,在其他人感兴趣的情况下,请在此添加。

请注意,如果通过某个终端手动连接,则可以在脚本中通过next发送select-X命令。但是,当然,我们不想手动附加。我们要使其自动化。

所以...为可憎做准备(:

我们将使用单独的屏幕会话将其附加到我们关注的屏幕会话,从而将该会话置于“已附加”状态,在该状态下,我们可以从主脚本向其发送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

虽然不漂亮,但是我知道我会用它的!

相关问题