无法将命令发送到分离的屏幕会话

无法将命令发送到分离的屏幕会话

我想向屏幕会话发送命令,如果可能的话获取输出。

我尝试向屏幕会话发送命令,就像我在这个网站和许多其他网站上找到的那样,但它似乎不起作用:

root@server [~]# screen -X "script -a -c 'ls -l' /tmp/command.log" && cat /tmp/command.log
cat: /tmp/command.log: No such file or directory
root@server [~]# 

请注意,只有 1 个 Screen 会话正在运行,因此我省略了 -S 和 -p (也尝试了这些,但没有效果)。例如:

root@server [~]# screen -p 0 -X stuff "script -a -c 'ls -l' /tmp/command.log" && cat /tmp/command.log
cat: /tmp/command.log: No such file or directory

答案1

首先,阅读将文本输入发送到分离的屏幕。您确实需要-p将输入定向到右侧窗口。此外,在您填充换行符(CR 或 LF,屏幕内运行的交互式 shell 接受两者)之前,该命令不会执行。那是:

screen -p 0 -X stuff "script -a -c 'ls -l' /tmp/command.log$(printf \\r)" &&
cat /tmp/command.log

还有第二个问题,即screen -X stuff …输入输入屏幕会话后命令就会完成。但运行该命令需要一点时间script。执行时cat /tmp/command.log,很可能还script没有完成;它甚至可能还没有开始。

您需要使内部运行的命令screen产生某种通知。例如,假设 Screen 中的 shell 与 Screen 运行在同一台计算机上,它可能会发回已完成的信号。

sh -c '
  sleep 99999999 &
  screen -p 0 -X stuff "\
script -a -c \"ls -l\" /tmp/command.log; kill -USR1 $!
"
  wait
  cat /tmp/command.log
'

相关内容