10

I have a screen session running with several windows. I want to attach to it, create a new screen window within it and start a shell in that new window.

Question: How can I do this from the command line outside the screen session?

I already tried a lot, e. g. screen -x (but it attaches to one of the existing screen windows; it does not create a new one nor does it start a new shell). Any hints are welcome.

Environment is Linux (Ubuntu 12.04).

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Alfe
  • 47,603
  • 17
  • 84
  • 139
  • Seems like there is no standard way of doing it. You can search in the [mailing list](http://lists.gnu.org/archive/html/screen-users/) – tuxuday May 15 '12 at 11:47
  • I would have accepted that as an answer because I found something there, thank you! :) – Alfe May 15 '12 at 12:21
  • @Alfe if you found a proper answer, post it yourself. You are allowed to accept your own answer after a while if it is a proper answer. Besides i'm curious :P – KurzedMetal May 15 '12 at 12:29

3 Answers3

6

I found something on the mailing list, thank you tuxuday :)

I'm doing it now this way:

#!/bin/bash
screen -X screen -t NEWWINDOW  # create new window (switches existing attached terminal)
sleep 0.1
screen -X other  # switch existing attached terminal back to its old window
sleep 0.1
gnome-terminal -e 'screen -x -p NEWWINDOW'  # connect to new window

I'm not sure about those sleeps, maybe they aren't necessary in all environments, but I can wait those 0.2s easily.

My .bash_aliases is changing the screen window title with a delay, so the awkward NEWWINDOW won't stick for long (and thus not hinder further calls to this script).

Alfe
  • 47,603
  • 17
  • 84
  • 139
  • 2
    The trick I got from the mailing list was the `screen -X other`. – Alfe May 15 '12 at 12:40
  • 1
    take a look at this [blog entry](https://rohieb.wordpress.com/2010/07/30/gnu-screen-start-with-multiple-windows-and-commands/). It recommends using a session command file (just a file with all the commands like `screen`, `select` and `other`) and run `screen -X source sessionfile` instead of calling `screen -X` multiple times. Good approach – KurzedMetal May 15 '12 at 13:18
  • I'm calling `screen -X` just twice … but if that script grows, the session command file approach is worth having a look at for sure :) – Alfe May 15 '12 at 13:34
  • Yes, you are right, i just pointed it because i found it neat. – KurzedMetal May 15 '12 at 13:51
5

Add new detached window to sesion_name and run command

screen -S sesion_name -x -X screen bash -c 'command; exec bash'
Yuriy Tumakha
  • 1,257
  • 14
  • 20
4

To choose a window to join, use the -p option. Giving + will create a new one, so your command is simply:

screen -x session_name -p +

This is documented on the man page:

-p n̲u̲m̲b̲e̲r̲_o̲r̲_n̲a̲m̲e̲|̲-̲|̲=̲|̲+̲

Preselect a window. This is useful when you want to reattach to a  
specific windor or you want to send a command via the "-X" option  
to a specific window. As with screen's select commant, "-" selects  
the blank window. As a special case for reattach, "=" brings up the  
windowlist on the blank window. 
zondo
  • 18,070
  • 7
  • 35
  • 73
  • I looked at my manpage again (version 4.00.03jw4) and found only the `-p number_or_name` (not the `|-|=|+`). Then I looked at a slightly newer version (4.01.00devel) and found your synopsis line. Confusing is that the text you pasted comes from the older man page (describing nothing about the `+`) while the newer manpage in my tests had this additional text describing the `+`: `… while a "+" will create a new window. The command will not be executed if the specified window could not be found.` So this is a new option! Thanks for pointing this out, I think it solves my issue. – Alfe Mar 05 '17 at 00:58