如何将东西命令发送到启动分离屏幕?
我以“-d -m”开始的任何会话都不接受“-X stuff [...]”命令,除非我至少附加到屏幕一次。没有错误消息,只是命令无法通过。
问题是我从 cron 作业启动会话,并且无法从 cron 内附加到屏幕。
重复步骤
$ screen -m -d -S mydaemon bash
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
32456.mydaemon (Detached)
$ screen -r -S mydaemon
$ ^a d
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
No Sockets found in /var/run/screen/S-user
^ad 表示按 Ctrl+a 然后按 d。
版本
CentOS release 5.5 (Final)
Screen version 4.00.03 (FAU) 23-Oct-06
Any session that I start with "-d -m" doesn't accept "-X stuff [...]" commands unless I've attached to the screen at least once. There is no error message, the commands just do not get through.
The problem is that I start the session from a cron job and am unable to attach to the screen from within cron.
Steps to repeat
$ screen -m -d -S mydaemon bash
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
32456.mydaemon (Detached)
$ screen -r -S mydaemon
$ ^a d
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
No Sockets found in /var/run/screen/S-user
^a d indicates pressing Ctrl+a then pressing d.
Versions
CentOS release 5.5 (Final)
Screen version 4.00.03 (FAU) 23-Oct-06
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:这个问题的最佳答案是这个其他答案。不管怎样,我把我的笨拙的解决方案留在这里,以防它激发类似问题的解决方案。
可能的解决方法是使用第二个已运行且分离的屏幕会话来启动要在附加模式下向其发送“stuff”命令的屏幕会话,然后向该屏幕会话发送分离命令,然后发送 stuff 命令。
(注意:双倍的“屏幕”不是是拼写错误!)您仍然会留下一个不可填充的屏幕会话(生成器),但如果它占用的资源很重要,您始终可以使用“kill -TERM” ...”或类似的方法来终止它(或者通过使用类似或类似的东西启动它在一段时间后自动退出
)。
Edit: The best answer to this question is this other SO answer. I leave my kludgey solution here, anyway, in case it inspires a solution to a similar problem.
A possible workaround is to use a second, already running and detached screen session to start the screen session to which you want to send the "stuff" command in attached mode, and then send that screen session a detach command and then the stuff command.
(Note: the doubled "screen" is not a typo!) You are still left with an unstuffable screen session (spawner) but if the resources it takes are important you can always just use "kill -TERM ..." or its ilk to terminate it (or have it automatically exit after a certain amount of time by starting it with something like
or similar).
您是否考虑过使用 tmux 来代替?它更适合非交互式任务。
Have you considered using
tmux
instead? It is much more suitable for non-interactive tasks.感谢罗恩·卡明斯基。
我找到了另一种方法来杀死“spawner”屏幕
并添加以下行
Thanks for Ron Kaminsky.
I found another way to kill 'spawner' screen
and add below line
screen (分离) (移动) (shell) “执行什么”
screen (detached) (mobile) (shell) "What to execute"
使用 .screenrc 文件,您可以设置要启动的内容;
因此,如果您想在窗口
7
中启动top
,您可以这样做Using the .screenrc file, you can set things to start;
So, if you wanted to start
top
in window7
, you'd do