启动屏幕(unix命令)+;在1个命令中运行命令?

启动屏幕(unix命令)+;在1个命令中运行命令?,unix,command-line,gnu-screen,Unix,Command Line,Gnu Screen,我想知道如何启动命令,例如: while :; do ./myCommand; done; 但不是照常做 screen -S nameOfMyScreen 然后命令 while :; do ./myCommand; done; 然后拆下屏幕 ^a ^d (Control "a" the control "d" 我希望它启动并分离。谢谢 从屏幕-h,这些看起来很有用: -dmS name Start as daemon: Screen session in detached mod

我想知道如何启动命令,例如:

while :; do ./myCommand; done;
但不是照常做

screen -S nameOfMyScreen
然后命令

while :; do ./myCommand; done;
然后拆下屏幕

^a ^d (Control "a" the control "d"

我希望它启动并分离。谢谢

屏幕-h
,这些看起来很有用:

-dmS name     Start as daemon: Screen session in detached mode.
-X            Execute <cmd> as a screen command in the specified session.
因此,
-X
开关可以执行屏幕命令,而不是shell命令。您可以将命令放在
-dmS
之后,而不需要任何
-X
开关

screen -d -m sh -c "while :; do ./myCommand; done;"
说明:

  • -d-m
    以分离模式启动屏幕(创建会话但不附加到会话)
  • sh-c命令行
    启动一个shell,该shell执行给定的命令行(必要时,因为您正在使用
    while
    内置命令行)

+1。这些组合将完成您的工作。由于您的命令是一个脚本,您可能需要将其放入一个文件中并传递该文件,或者使用
bash-c
或类似的东西。我的系统上的screen版本说-c选项是读取一个备用配置文件,而不是.screenrc。
-c
不会传递给screen,而是传递给
sh
。在屏幕命令行解析器读取
sh
(即没有破折号的内容)后,它知道没有更多选项,其他所有内容都是应该执行的命令。感谢shell技巧。我最初调用的是一个命令,但我想
tee
将输出添加到包含stderr的日志文件中。。。当我添加管道和重定向时,日志将是空的,可能是从
screen
而不是我的命令获取输出。我试图添加引号,但没有成功。使用此处建议的子shell非常有效\o/
screen -d -m sh -c "while :; do ./myCommand; done;"