in Screen, how do I send a command to all virtual terminal windows within a single screen session? [closed]

我们两清 提交于 2019-12-02 15:39:01
Corey Henderson

I found a good tutorial here to do this:

http://blog.cone.be/2009/11/24/gnu-screen-nethack-different-screen-windows-sending-commands-to-all-screen-windows/

From the post:

Once you re used to the multiple windows, you might run into a situation where you want to send a same command to several of these open windows. Screen provides in the “at” command to do this. First you ll need to open command line mode.

C-a : (colon) Enter command line mode.

This way you can type a command once, but you ll still have to enter each separate window. But there is a better way. As an example we ‘ll send “ls -l” to all the windows.

at "#" stuff "ls -l^M"

This command is barely readable, so let's pick it apart! The first part is 'at [identifier][#|*|%] command'. The at command sends the text parameter to all the windows you specified in the identifier. You can match the criteria to either the window name or number with #, the user name with * or the displays, using %. The next part is the command you want to run in the selected windows. We’re using "stuff" to stuff the command we want to execute into the input buffer of the selected windows. Stuff is really straightforward. It simply stuffs the string you gave as a parameter. Next problem is the command. Or rather having it executed! To get screen to put an “enter” after the command, to execute the command, add “^M” at the end. You can do a lot more with this than just sending an ls to the input. Any screen command, like renaming, moving windows around, whatnot .. is available in combination with "at".

Sorry for this belated reply, but tmux might be a better choice for you than screen. In tmux, you have to press: C-b : to enter the command mode and input: setw synchronize-panes (or just setw sync<Tab> using autocompletion). Note that this command enables synchronization between panes (areas of one split screen, visible simultaneously), but not between windows (full, not split, screens).

You may also want to send to selected windows only (multicast).

  1. Convention: Give 1st character of window title the meaning of a broadcast flag. E.g. if title starts with "." then the window listens to broadcast, otherwise it doesn't.
  2. Set titles as needed.

    screen -S SessionName -p 0 -X title "remote_0"     # window 0: ignore multicast
    screen -S SessionName -p 1 -X title ".remote_1"    # window 1: listen to multicast
    
  3. Send contents to listeners with the at command of screen

    screen -S SessionName -X at ".#" stuff "date
    "
    
  4. Note: the ending double quote above ensures a ^M to be sent (Cr).

  5. You may enable / disable multicast based on window title this way.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!