Linux screen detach attached screen

How to Use Screen in Linux [List, Attach, Detach, Close]

Screen is an application that allows you to create multiple shell sessions. When you open a new shell session using screen, screen creates a new process in the background. Even if you leave this session with the «Ctrl + a + d» combination, the screen command continues to run the session in the background. It allows you to connect to the same session again later.

This article covers Screen command setup and «How to Use Screen on Linux?» includes topics.

Installing Screen utility on Linux

The setup on Redhat based operating systems(Fedora, Centos, AlmaLinux, Rocky Linux,etc) is as follows:

sudo dnf -y install screen
sudo yum -y install screen

In Debian-based operating systems (Ubuntu, Pardus, Linux Mint, Kali Linux, etc.) it is as follows:

sudo apt install screen -y
sudo apt-get install screen -y
sudo emerge -a sys-apps/screen
sudo zypper install screen

How to Use Screen on Linux?

The screen command is most often used to ensure that a long-running script does not terminate even if the terminal is closed.

After installation, you can login to the application by typing screen in the terminal:

screen

To end the Screen session, you must type exit while logged in:

foc@fedora:~$ exit foc@fedora:~$ screen [screen is terminating]

10 Common Screen Command Examples

Let’s explain the 10 most used screen commands you will need, with examples.

1. List Screen Sessions

These two commands can be used to list previously opened screen sessions. Both give the same screen output:

foc@fedora:~$ screen -list There is a screen on: 9846.pts-6.fedora (Attached) 9316.golinuxcloud (Attached) 9074.pts-1.fedora (Attached) 3 Sockets in /run/screen/S-foc.
foc@fedora:~$ screen -ls There are screens on: 9846.pts-6.fedora (Attached) 9074.pts-1.fedora (Attached) 2 Sockets in /run/screen/S-foc.

2. Connect to previous screen session

Type the session you want to connect from the listed screen sessions as follows:

foc@fedora:~$ screen -a -r 9074.pts-1.fedora

3. Close the open screen session

The -X and -S parameters are used to end the open session. The session id is followed by «quit»:

foc@fedora:~$ screen -X -S 9074.pts-0.fedora quit

4. Switching between screen sessions

To switch between sessions,

5. Name the newly opened session

In the new screen session, give a name with the -S parameter. This name can define the work done in that session.

foc@fedora:~$ screen -S golinuxcloud

Then you can see the session opened with this name:

foc@fedora:~$ screen -ls There are screens on: 13350.golinuxcloud (Attached) 11997.pts-3.fedora (Attached) 2 Sockets in /run/screen/S-foc.

6. Detach last screen session (Screen Disconnect)

«CTRL + A + D» does the same job as the key combination. After this command, the session ends.

foc@fedora:~$ screen -d 13350.golinuxcloud [13350.golinuxcloud detached.]

The latest status of the session is as follows:

foc@fedora:~$ screen -ls There are screens on: 13350.golinuxcloud (Detached) 11997.pts-3.fedora (Attached) 

7. Reattach the screen session

It is used to reattach a screen session which was detached in past. Let’s connect to the 13350.golinuxcloud session:

foc@fedora:~$ screen -d 13350.golinuxcloud

The latest status of the session:

foc@fedora:~$ screen -ls There are screens on: 13350.golinuxcloud (Attached) 11997.pts-3.fedora (Attached)

8. Lock the Screen Session

If you want to lock the Screen session at that moment, use the CTRL + a + x switch combination.

Screen used by foc on fedora. Password:

9. Take the log of the Screen session

Use the -L parameter when starting the Screen session. The screenlog.0 file will be formed in the directory where you started the session.

foc@fedora:/tmp$ ls -la screenlog.0 -rw-r--r--. 1 foc foc 72 Oct 24 20:58 screenlog.0

10. Set the name of the window

Give the window a name with the » -t » parameter. In previous sessions, the default «[Screen 0: Bash]» value is replaced with the new value. The image of the window in the last case:

Читайте также:  Hp laserjet pro mfp m428fdn драйвер astra linux

How to Use Screen in Linux [List, Attach, Detach, Close]

Summary

When it is in the screen session, CTRL+A activates switch connections. Some are as follows:

key binding ? Show key bindings. A Set a title for the current window. c Create a new window. n Switch to the next window. p Switch to the previous window. 0-9 Switch to window number n. w Show a list of windows. " Present a list of windows for selection. F Resize the window to the current region size. [ or esc Enter copy/scrollback mode. k Destroy current window. 

For more details about Screen, run the following command at the terminal:

foc@fedora:~$ screen -help Use: screen [-opts] [cmd [args]] or: screen -r [host.tty] Options: -a Force all capabilities into each window's termcap. -A -[r|R] Adapt all windows to the new display width & height. -c file Read configuration file instead of '.screenrc'. -d (-r) Detach the elsewhere running screen (and reattach here). -dmS name Start as daemon: Screen session in detached mode. . 

What’s Next

You should also check screen alternative in newer distributions i.e List of 50+ tmux cheatsheet and shortcuts commands

References

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Can’t resume screen, says I am already attached?

I am working on a remote Debian Jessie server. I have started a screen session, started running a script, then been disconnected by a network timeout. Now I have logged in again and want to resume the session. This is what I see when I list screens:

$ screen -ls There are screens on: 30608.pts-8.myserver (11/03/2015 08:47:58 AM) (Attached) 21168.pts-0.myserver (11/03/2015 05:29:24 AM) (Attached) 7006.pts-4.myserver (10/23/2015 09:05:45 AM) (Detached) 18228.pts-4.myserver (10/21/2015 07:50:49 AM) (Detached) 17849.pts-0.myserver (10/21/2015 07:43:53 AM) (Detached) 5 Sockets in /var/run/screen/S-me. 

I seem to be attached to two screens at once. Now I want to resume the session I was running before, to see the results of my script:

$ screen -r 30608.pts-8.myserver There is a screen on: 30608.pts-8.OpenPrescribing (11/03/2015 08:47:58 AM) (Attached) There is no screen to be resumed matching 30608.pts-8.myserver. 
$ screen -r 21168.pts-0.myserver There is a screen on: 21168.pts-0.OpenPrescribing (11/03/2015 05:29:24 AM) (Attached) There is no screen to be resumed matching 21168.pts-0.myserver. 

4 Answers 4

The session is still attached on another terminal. The server hasn’t detected the network outage on that connection: it only detects the outage when it tries to send a packet and gets an error back or no response after a timeout, but this hasn’t happened yet. You’re in a common situation where the client detected the outage because it tried to send some input and failed, but the server is just sitting there waiting for input. Eventually the server will send a keepalive packet and detect that the connection is dead.

Читайте также:  Linux proxy udp proxy

In the meantime, use the -d option to detach the screen session from the terminal where it’s in.

screen -rd is pretty much the standard way to attach to an existing screen session.

To attach to an existing screen session, it’s screen -x , screen -rd is to attach to it after having detached it from every terminal it was attached to. In the case of the OP, I’d agree the latter is probably prefered, but in the general case, not necessarily. I generally use -xR to attach or create if there’s nothing to attach.

According to the manual you can «resume» only detached screens:

-ls [match]
-list [match]
Do not start screen, but instead print a list of session identification strings (usually of the form pid.tty.host; see Session Name). Sessions marked ‘detached’ can be resumed with screen -r . Those marked ‘attached’ are running and have a controlling terminal. If the session runs in multiuser mode, it is marked ‘multi’. Sessions marked as ‘unreachable’ either live on a different host or are dead. An unreachable session is considered dead, when its name matches either the name of the local host, or the specified parameter, if any. See the -r flag for a description how to construct matches. Sessions marked as ‘dead’ should be thoroughly checked and removed. Ask your system administrator if you are not sure. Remove sessions with the ‘ -wipe ’ option.

What you want is this option:

-x
Attach to a session which is already attached elsewhere (multi-display mode). Screen refuses to attach from within itself. But when cascading multiple screens, loops are not detected; take care.

Источник

Читайте также:  Hp laserjet pro mfp m428fdn драйвер astra linux

How do I force detach Screen from another SSH session?

As Jose answered, screen -d -r should do the trick. This is a combination of two commands, as taken from the man page.

screen -d detaches the already-running screen session, and screen -r reattaches the existing session. By running screen -d -r , you force screen to detach it and then resume the session.

If you use the capital -D -RR , I quote the man page because it’s too good to pass up.

Attach here and now. Whatever that means, just do it.

Note: It is always a good idea to check the status of your sessions by means of «screen -list».

You can also do screen -x which is faster to type, but not entirely equivalent. It will attach the session a second time without detaching first, resulting in multi display mode.

try with screen -d -r or screen -D -RR

Short answer

  1. Reattach without ejecting others: screen -x
  2. Get list of displays: ^A * , select the one to disconnect, press d

Explained answer

Background: When I was looking for the solution with same problem description, I have always landed on this answer. I would like to provide more sensible solution. (For example: the other attached screen has a different size and a I cannot force resize it in my terminal.)

  • «user front-end» (in at command manual in screen)
  • «client» (tmux vocabulary where this functionality is detach-client )
  • «terminal» (as we call the window in our user interface) /depending on

1. Reattach a session: screen -x

-x attach to a not detached screen session without detaching it

2. List displays of this session: PREFIX *

It is the default key binding for: PREFIX :displays . Performing it within the screen, identify the other display we want to disconnect (e.g. smaller size). (Your current display is displayed in brighter color/bold when not selected).

term-type size user interface window Perms ---------- ------- ---------- ----------------- ---------- ----- screen 240x60 you@/dev/pts/2 nb 0(zsh) rwx screen 78x40 you@/dev/pts/0 nb 0(zsh) rwx 

Using arrows ↑ ↓ , select the targeted display, press d If nothing happens, you tried to detach your own display and screen will not detach it. If it was another one, within a second or two, the entry will disappear.

Press ENTER to quit the listing.

Optionally: in order to make the content fit your screen, reflow: PREFIX F (uppercase F)

Excerpt from man page of screen:

  • mouseclick Move to the selected line. Available when «mousetrack» is set to on.
  • space Refresh the list
  • d Detach that display
  • D Power detach that display
  • C-g , enter , or escape Exit the list

Источник

Оцените статью
Adblock
detector