如何创建执行给定命令的屏幕?

发布于 2024-11-29 09:45:01 字数 270 浏览 0 评论 0原文

我对 *nix 还很陌生。有没有办法创建一个屏幕,它将立即执行给定的命令序列(带有自己的参数)?两个小时的谷歌搜索一无所获——也许是因为我不能 清楚地陈述问题。

我希望像

screen -dmS new_screen exec "cd /dir && java -version"

我使用 screen v4.00.03 和 CentOS 5.5 (内核版本 2.6.18-194.26.1.el5.028stab079.2)

i'm fairly new in *nix. Is there a way to create a screen, which will immediately execute a given command sequence (with their own arguments)? Two hours of googling yields nothing - perhaps because I can't
clearly state the question.

I hope for something like

screen -dmS new_screen exec "cd /dir && java -version"

I am using screen v4.00.03 and CentOS 5.5 (kernel ver. 2.6.18-194.26.1.el5.028stab079.2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

佼人 2024-12-06 09:45:01

您创建一个具有名称并处于分离模式的屏幕:

screen -S "mylittlescreen" -d -m

然后发送要在屏幕上执行的命令:

screen -r "mylittlescreen" -X stuff 

stuff 命令用于在屏幕内发送击键。字符串命令前的 $ 是为了让 shell 解析引号内的 \n,执行命令时需要换行符(就像按 Enter 时一样)。

这在这个屏幕版本上对我有用:

$ 屏幕-v

屏幕版本 4.00.03jw4 (FAU) 2006 年 5 月 2 日

有关命令的详细信息,请参阅 man screen

ls\n'

stuff 命令用于在屏幕内发送击键。字符串命令前的 $ 是为了让 shell 解析引号内的 \n,执行命令时需要换行符(就像按 Enter 时一样)。

这在这个屏幕版本上对我有用:

$ 屏幕-v

屏幕版本 4.00.03jw4 (FAU) 2006 年 5 月 2 日

有关命令的详细信息,请参阅 man screen

You create a screen with a name and in detached mode:

screen -S "mylittlescreen" -d -m

Then you send the command to be executed on your screen:

screen -r "mylittlescreen" -X stuff 

The stuff command is to send keystrokes inside the screen. The $ before the string command is to make the shell parse the \n inside the quotes, and the newline is required to execute the command (like when you press enter).

This is working for me on this screen version:

$ screen -v

Screen version 4.00.03jw4 (FAU) 2-May-06

Please see man screen for details about the commands.

ls\n'

The stuff command is to send keystrokes inside the screen. The $ before the string command is to make the shell parse the \n inside the quotes, and the newline is required to execute the command (like when you press enter).

This is working for me on this screen version:

$ screen -v

Screen version 4.00.03jw4 (FAU) 2-May-06

Please see man screen for details about the commands.

乖乖兔^ω^ 2024-12-06 09:45:01

问题是使用“exec”屏幕命令不会启动 shell。 'cd' 是 shell 内置命令,因此您需要一个 shell。另外,您需要一个保持运行的 shell,以便屏幕不会终止。

您可以使用 screen 的 -X 选项将命令发送到正在运行的屏幕会话,并使用“stuff”命令将击键发送到当前窗口。试试这个:

screen -dmS new_screen sh
screen -S new_screen -X stuff "cd /dir
"
screen -S new_screen -X stuff "java -version
"

是的,您需要在下一行加上引号才能执行命令。

The problem is that using the 'exec' screen command does not start a shell. 'cd' is a shell builtin, so you need a shell for it. Also, you need a shell that remains running so that screen does not terminate.

You can use the -X option to screen to send commands to a running screen session, and the 'stuff' command to send keystrokes to the current window. Try this:

screen -dmS new_screen sh
screen -S new_screen -X stuff "cd /dir
"
screen -S new_screen -X stuff "java -version
"

Yes, you need to put the quotes on the next line in order for the commands to be executed.

久而酒知 2024-12-06 09:45:01

screen -dmS screen_name bash -c 'sleep 100'

这将创建名为 screen_name 的新屏幕。在屏幕内它将休眠 100 秒。

请注意,如果您键入一些命令来代替 sleep 100,该命令在执行后立即终止,屏幕也会终止。 因此您将无法看到刚刚创建的屏幕

screen -dmS screen_name bash -c 'sleep 100'

This will create new screen named screen_name. And inside the screen it will sleep for 100 seconds.

Note that if you type some command in place of sleep 100 which terminates immediately upon execution, the screen will terminate as well. So you wont be able to see the screen you just created

苍景流年 2024-12-06 09:45:01

我想从 bash 脚本内启动远程屏幕,并在 bash 脚本内定义一些变量并在屏幕内可用。所以对我有用的是

#!/bin/bash
SOMEVAR1="test2"
# quit existing if there is one running already, be careful
screen -D -RR test1 -X quit || true
screen -dmS test1
screen -r test1 -p 0 -X stuff $"echo ${SOMEVAR1} ^M"

返回字符 ^M,你需要使用 vim 作为输入

i CTRL-V ENTER ESCAPE

I wanted to launch remote screens from within a bash script with some variables defined inside the bash script and available inside screen. So what worked for me was

#!/bin/bash
SOMEVAR1="test2"
# quit existing if there is one running already, be careful
screen -D -RR test1 -X quit || true
screen -dmS test1
screen -r test1 -p 0 -X stuff $"echo ${SOMEVAR1} ^M"

Where the return character, ^M, you need to enter using vim as

i CTRL-V ENTER ESCAPE
独﹏钓一江月 2024-12-06 09:45:01

另一种方法

第一行 cd 到您的目录。
第二行使用 bash 启动一个名为 new_screen 的新屏幕会话。
第三行执行java -version

cd /dir
screen -dmS new_screen bash
screen -S new_screen -p 0 -X exec java -version

Another approach

First line cd to the your directory.
Second line start a new screen session named new_screen with bash.
Third line executing java -version

cd /dir
screen -dmS new_screen bash
screen -S new_screen -p 0 -X exec java -version
滥情哥ㄟ 2024-12-06 09:45:01

我认为你可以使用这个

function exec_in_screen() {
  name=$1
  command=$2
  screen -dmS $name sh; screen -S $name -X stuff "$command\n";
} 

然后...

exec_in_screen "test" "ls"

I think that you can use this

function exec_in_screen() {
  name=$1
  command=$2
  screen -dmS $name sh; screen -S $name -X stuff "$command\n";
} 

Then...

exec_in_screen "test" "ls"

盛夏已如深秋| 2024-12-06 09:45:01

是的,您想要的是“stuff”命令

,例如

screen -dmS new_screen -X stuff "cd /dir && java -version

"

第二个引号位于下一行,以便在发送时执行

Yes, what you want is the "stuff" command

for example

screen -dmS new_screen -X stuff "cd /dir && java -version

"

the second quote is on the next line so that it executes when sent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文