Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach to 'screen' session with creating a new screen window

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).

like image 218
Alfe Avatar asked May 15 '12 11:05

Alfe


People also ask

How do I reattach a screen session to another?

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.

How to create a session inside a session in 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.

How to create nested screen sessions in Windows 10?

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.

How to detach and reattach to a named session?

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>


2 Answers

Add new detached window to sesion_name and run command

screen -S sesion_name -x -X screen bash -c 'command; exec bash'
like image 120
Yuriy Tumakha Avatar answered Nov 15 '22 18:11

Yuriy Tumakha


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. 
like image 43
zondo Avatar answered Nov 15 '22 17:11

zondo