bash 如何使用命令启动 Unix screen 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/543737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:56:51  来源:igfitidea点击:

How do you start Unix screen command with a command?

bashunixshellcommand-linegnu-screen

提问by Ethan

According to the docs for the Unix "screen" command, you can configure it in .screenrc to start with a bunch of default screens, each running a command that you specify.

根据Unix“屏幕”命令文档,您可以在 .screenrc 中配置它以从一堆默认屏幕开始,每个屏幕运行您指定的命令。

Here's my cofig:

这是我的配置文件:

# Default screens
screen -t "shell_0"  1
screen -t "autotest" 2 cd ~/project/contactdb ; autotest

It will not run the autotest command. That window where I'm trying to run autotestjust closes instantly when I start screen.

它不会运行自动测试命令。autotest当我开始时,我试图运行的那个窗口会立即关闭screen

I also tried it with just...

我也试过只...

screen -t "autotest" 2 cd ~/project/contactdb

Same result.

结果一样。

I also tried...

我也试过...

screen -t "autotest" 2 ls

Same result there too.

那里也有同样的结果。

What's the secret to getting it to run a command in a given screen on startup?

让它在启动时在给定屏幕中运行命令的秘诀是什么?

采纳答案by chaos

Your program is being run (well, except the cd), it's just that it's being run without a parent shell, so as soon as it completes, it exits and you're done.

你的程序正在运行(嗯,除了 cd),只是它在没有父 shell 的情况下运行,所以一旦它完成,它就会退出,你就完成了。

You could do:

你可以这样做:

screen -t "autotest" 2 bash -c 'cd ~/project/contactdb ; autotest'

Spawns two shells, but life will probably go on.

生成两个贝壳,但生活可能会继续。

回答by sodamnmad

Try this:

尝试这个:

$ screen -S 'tailf messages' -d -m tailf /var/log/messages

Then later you can do:

然后你可以这样做:

$ screen -ls
1234.tailf messages

Followed by:

其次是:

$screen -r 1234

回答by Otis

This might help but may not be entirely what you want.

这可能会有所帮助,但可能不完全是您想要的。

Put "zombie az" or "defzombie az" as the first line of your .screenrc. "az" can be whatever 2 keys you'd like. Now, when a screen ought to close (command finished executing, for instance), it won't actually close; hitting 'a' will close it, hitting 'z' will re-execute the command attached to that screen.

将“zombie az”或“defzombie az”作为 .screenrc 的第一行。“az”可以是您想要的任何 2 个键。现在,当一个屏幕应该关闭时(例如命令完成执行),它实际上不会关闭;点击“a”将关闭它,点击“z”将重新执行附加到该屏幕的命令。

I found that at the screen user's manual.

我在屏幕用户手册中发现了这一点。

回答by Jamie Flournoy

You can also "stuff" characters into the screen as if you had typed them.

您还可以将字符“填充”到屏幕中,就像您输入它们一样。

Here's how you can do that with your example:

以下是您如何用您的示例做到这一点:


screen -t "shell_0"  1

# create the following screen in the desired dir, instead of cd-ing afterwards :)
chdir ~/project/contactdb
screen -t "autotest" 2

# (without this sometimes screens fail to start correctly for me)
sleep 5

# paste some text into screen number 2:
select 2
stuff "autotest2"

回答by Jamie Flournoy

Here's how mine looks. It seems to work fine. I think either the parenthesis might be causing the problem or screen will not open a window if the command "autotest" does not exist.

这是我的样子。它似乎工作正常。我认为如果“自动测试”命令不存在,括号可能会导致问题,或者屏幕将不会打开窗口。

screen -t zsh 1
screen -t emacs 2 emacs -nw
screen -t mutt 3 mutt
monitor on
screen -t mc 4 mc -s
screen -t elinks 4 elinks
screen -t zsh 1
screen -t emacs 2 emacs -nw
screen -t mutt 3 mutt
monitor on
screen -t mc 4 mc -s
screen -t elinks 4 elinks

回答by Adam Monsen

Here's how I'd do it.

这就是我要怎么做。

screen -t shell_0
chdir ~/project/contactdb
screen -t autotest autotest

The above appears to be evaluated procedurally by screen. First we establish a new screen with the title shell_0. Since we gave no other options, current working directory will be that of the parent shell or the user's home directory. We then set the default directory for new screens to ~/project/contactdb. Next, we establish a new screen running the autotestcommand.

以上似乎是通过屏幕程序评估的。首先,我们建立一个标题为 的新屏幕shell_0。由于我们没有提供其他选项,当前工作目录将是父 shell 或用户的主目录。然后我们将新屏幕的默认目录设置为~/project/contactdb. 接下来,我们建立一个运行autotest命令的新屏幕。

Window number (n) is optional, I generally omit it.

窗口号 ( n) 是可选的,我一般省略它。