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).
The solution is to run the screen command with -rd: This tells screen to first detach the session and then reattach to it. Another solution is to run the screen command with -x: This tells screen to reattach to an already attached session. Happy hackin’! Tags: screen.
We can, however, create nested sessions (a session inside a session). First, create a new session or attach to an opened session. I am going to create a new session named "nested". Now, press Ctrl+a and c inside the session to create another session. Just repeat this to create any number of nested Screen sessions.
First, create a new session or attach to an opened session. I am going to create a new session named "nested". Now, press Ctrl+a and c inside the session to create another session. Just repeat this to create any number of nested Screen sessions. Each session will be assigned with a number.
Use Ctrl+a + d to detach from the screen session Reattach to screen session by typing screen -r <ID> Start a named session using screen -S <name> Reattach to a named screen session by typing screen -r <name>
Add new detached window to sesion_name
and run command
screen -S sesion_name -x -X screen bash -c 'command; exec bash'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With