如何将东西命令发送到启动分离屏幕?

发布于 2024-10-07 17:03:26 字数 598 浏览 4 评论 0原文

我以“-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 技术交流群。

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

发布评论

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

评论(5

叹倦 2024-10-14 17:03:26

编辑:这个问题的最佳答案是这个其他答案。不管怎样,我把我的笨拙的解决方案留在这里,以防它激发类似问题的解决方案。


可能的解决方法是使用第二个已运行且分离的屏幕会话来启动要在附加模式下向其发送“stuff”命令的屏幕会话,然后向该屏幕会话发送分离命令,然后发送 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"

(注意:双倍的“屏幕”不是是拼写错误!)您仍然会留下一个不可填充的屏幕会话(生成器),但如果它占用的资源很重要,您始终可以使用“kill -TERM” ...”或类似的方法来终止它(或者通过使用类似或类似的东西启动它在一段时间后自动退出

$ screen -dmS spawner bash -c "sleep 60"

)。

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.

$ 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

$ screen -dmS spawner bash -c "sleep 60"

or similar).

我还不会笑 2024-10-14 17:03:26

您是否考虑过使用 tmux 来代替?它更适合非交互式任务。

Have you considered using tmux instead? It is much more suitable for non-interactive tasks.

帅冕 2024-10-14 17:03:26

感谢罗恩·卡明斯基。

我找到了另一种方法来杀死“spawner”屏幕

    $ 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 screen screen -S spawner -dR
    $ sleep 1 # may be necessary
    $ screen -S spawner -X detach
    $ screen -S spawner -X kill

Thanks for Ron Kaminsky.

I found another way to kill 'spawner' screen

    $ 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
生来就爱笑 2024-10-14 17:03:26

屏幕-d -m /bin/bash“test.sh”

screen (分离) (移动) (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.

screen -d -m /bin/bash "test.sh"

screen (detached) (mobile) (shell) "What to execute"

$ 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.
郁金香雨 2024-10-14 17:03:26

使用 .screenrc 文件,您可以设置要启动的内容;

screen -t <title> <window position> <command>

因此,如果您想在窗口 7 中启动 top,您可以这样做

screen -t window7top 7 top

Using the .screenrc file, you can set things to start;

screen -t <title> <window position> <command>

So, if you wanted to start top in window 7, you'd do

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