[英]How can I send stuff commands to a start-in-detached screen?
Any session that I start with "-d -m" doesn't accept "-X stuff [...]" commands unless I've attached to the screen at least once. 我以“-d -m”开头的任何会话都不接受“-X stuff [...]”命令,除非我已经连接到屏幕至少一次。 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. 问题是我从cron作业启动会话,无法从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
^ad indicates pressing Ctrl+a then pressing d. ^ ad表示按Ctrl + a然后按d。
Versions 版本
CentOS release 5.5 (Final)
Screen version 4.00.03 (FAU) 23-Oct-06
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.
无论如何,我将我的kludgey解决方案留在这里,以防它激发解决类似问题的方法。
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. 一种可能的解决方法是使用第二个已经运行和分离的屏幕会话来启动要在附加模式下发送“stuff”命令的屏幕会话,然后向该屏幕会话发送一个detach命令,然后发送stuff命令。
$ screen -dmS spawner
$ screen -S spawner -X screen screen -dR mydaemon
$ sleep 1 # may be necessary
$ screen -S mydaemon -X detach
$ screen -S mydaemon -X stuff "whatever"
(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 (注意:加倍的“屏幕” 不是拼写错误!)你仍然留下了一个无法解决的屏幕会话(spawner)但是如果它所需的资源很重要你总是可以使用“kill -TERM ...”或它的同类终止它(或者在一段时间之后通过类似的东西启动它自动退出它
$ screen -dmS spawner bash -c "sleep 60"
or similar). 或类似的)。
Thanks for Ron Kaminsky. 感谢Ron Kaminsky。
I found another way to kill 'spawner' screen 我发现了另一种杀死'spawner'屏幕的方法
$ screen -dmS spawner
$ screen -S spawner -X screen screen -dR mydaemon
$ sleep 1 # may be necessary
$ screen -S mydaemon -X detach
and add below line 并添加以下行
$ screen -S mydaemon -X screen screen -S spawner -dR
$ sleep 1 # may be necessary
$ screen -S spawner -X detach
$ screen -S spawner -X kill
screen -d -m /bin/bash "test.sh"
屏幕-d -m / bin / bash“test.sh”
screen (detached) (mobile) (shell) "What to execute" 屏幕(分离)(移动)(shell)“执行什么”
$ screen -d -m /bin/bash "test.sh"
$ screen -li
There is a screen on:
8540..Satelite (10/17/2013 04:53:22 AM) (Detached)
1 Socket in /var/run/screen/S-matt.
Have you considered using tmux
instead? 您是否考虑过使用
tmux
? It is much more suitable for non-interactive tasks. 它更适合非交互式任务。
Using the .screenrc file, you can set things to start; 使用.screenrc文件,您可以设置要启动的东西;
screen -t <title> <window position> <command>
So, if you wanted to start top
in window 7
, you'd do 所以,如果你想开始
top
在窗口7
,你会怎么做
screen -t window7top 7 top
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.