将命令发送到 GNU 屏幕
我有一个名为 demo 的 GNU 屏幕,我想向它发送命令。我该怎么做?
screen -S demo -X /home/aa/scripts/outputs.sh
yeilds No screen session found.
并且执行 screen -ls
显示它没有运行。
I have a GNU screen named demo, I want to send commands to it. How do I do this?
screen -S demo -X /home/aa/scripts/outputs.sh
yeilds No screen session found.
and doing screen -ls
shows that it isn't running.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 Screen 会话未运行,您将无法向其发送内容。先开始吧。
建立会话后,您需要区分屏幕命令和键盘输入。
screen -X
需要 Screen 命令。stuff
命令发送输入,如果您想从 shell 提示符运行该程序,则还必须传递换行符。请注意,这可能是错误的方法。您确定要输入该会话中活动的内容吗?要将输入定向到特定窗口,请使用
其中 1 是窗口编号(您可以使用其标题)。
要在该会话中启动新窗口,请改用
screen
命令。 (这是screen
Screen 命令,而不是screen
shell 命令。)If the Screen session isn't running, you won't be able to send things to it. Start it first.
Once you've got a session, you need to distinguish between Screen commands and keyboard input.
screen -X
expects a Screen command. Thestuff
command sends input, and if you want to run that program from a shell prompt, you'll have to pass a newline as well.Note that this may be the wrong approach. Are you sure you want to type into whatever is active in that session? To direct the input at a particular window, use
where 1 is the window number (you can use its title instead).
To start a new window in that session, use the
screen
command instead. (That's thescreen
Screen command, not thescreen
shell command.)我将它们放在一起以捕获命令的输出。如果您想通过管道传输某些输入,它还可以处理标准输入。
更进一步,通过 ssh 调用此函数可能很有用:
也许将其与
ssh user@host 之类的东西结合起来"$(typeset -f xscreen); xscreen ..."
因此您不必在远程主机上定义该函数。bash 脚本中的较长版本,用于处理返回状态和语法错误:
I put this together to capture the output from the commands. It also handles stdin if you want to pipe some input.
Taking it a step further, it can be useful to call this function over ssh:
Maybe combine it with something like
ssh user@host "$(typeset -f xscreen); xscreen ..."
so you don't have to have the function already defined on the remote host.A longer version in a bash script that handles the return status and syntax errors: