Use of the screen command in Linux
Note that the screen command is not installed in rhel6 by default:
[root@rhel63single yum.repos.d]# yum install screenLoaded plugins: product-id, refresh-packagekit, security, subscription-managerUpdating certificate-based repositories.Unable to read consumer identityrhel-source | 4.0 kB 00:00 ... rhel-source-beta | 4.0 kB 00:00 ... Setting up Install ProcessResolving Dependencies--> Running transaction check---> Package screen.x86_64 0:4.0.3-16.el6 will be installed--> Finished Dependency ResolutionDependencies Resolved============================================================================================================================================================ Package Arch Version Repository Size============================================================================================================================================================Installing: screen x86_64 4.0.3-16.el6 rhel-source 494 kTransaction Summary============================================================================================================================================================Install 1 Package(s)Total download size: 494 kInstalled size: 795 kIs this ok [y/N]: yDownloading Packages:Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning Transaction Installing : screen-4.0.3-16.el6.x86_64 1/1 Installed products updated. Verifying : screen-4.0.3-16.el6.x86_64 1/1 Installed: screen.x86_64 0:4.0.3-16.el6 Complete![root@rhel63single yum.repos.d]# which screen/usr/bin/screen[root@rhel63single yum.repos.d]#
1. Create screen
[Root @ rhel63single ~] # Screen-lsNo Sockets found in/var/run/screen/S-root. [root @ rhel63single ~] # [Root @ rhel63single ~] # [Root @ rhel63single ~] # Screen-dmS lei1 [root @ rhel63single ~] # Screen-lsThere is a screen on: 5786. lei1 (Detached) 1 Socket in/var/run/screen/S-root. [root @ rhel63single ~] #5786 is the ospid of linux. Lei1 is the name of the session.
2. screen entry
[root@rhel63single ~]# screen -r lei1
3. Temporarily exit a screen session: press ctrl, press a in lower case, and then press d in lower case.
[root@rhel63single ~]# [detached][root@rhel63single ~]#
4. if it is already in this session, use the screen-r lei1 command again:
[Root @ rhel63single ~] # Screen-r lei or use screen-r 5786 There is a screen on: 5786. lei1 (Attached) There is no screen to be resumed matching lei. [root @ rhel63single ~] #
5. status explanation:
[Root @ rhel63single ~] # Screen-dmS lei2 [root @ rhel63single ~] # [Root @ rhel63single ~] # [Root @ rhel63single ~] # [Root @ rhel63single ~] # Screen-lsThere are screens on: 5850. lei2 (Detached) ----> suspended, no terminal in connection session 5786. lei1 (Attached) ----> there is a terminal in the connection session. 2 Sockets in/var/run/screen/S-root. [root @ rhel63single ~] #
6. Permanently exit the current session in the screen.
[root@rhel63single ~]# exit exit [screen is terminating][root@rhel63single ~]# screen -lsThere is a screen on: 5850.lei2 (Detached)1 Socket in /var/run/screen/S-root.
7. Check the source ip addresses of other sessions in screen:
[Root @ rhel63single ~] # Who-a system boot run-level 5 12: 36 LOGIN tty2 2015-03-19 12:37 1979 id = 2 LOGIN tty4 2015-03-19 12:37 1983 id = 4 LOGIN tty3 2015-03-19 12:37 1981 id = 3 LOGIN tty5 2015-03-19 12:37 1985 id = 5 LOGIN tty6 2015-03-19 1987 id = 6 root + pts/0 2015-03-19 5306 (192.168.80.1) pts/1 2015-03-19 5787 id =/1 term = 0 exit = 0 pts/2 2015-03-19 5810 id = ts/2 term = 0 exi T = 0 root + pts/3 2015-03-19. 5830 (192.168.80.1) root + pts/4 2015-03-19 5851 (192.168.80.1: S.0) ----> note here [root @ rhel63single ~] # Screen-lsThere is a screen on: 5850. lei2 (Attached) 1 Socket in/var/run/screen/S-root. [root @ rhel63single ~] # Ps-ef | grep 5851 ----> note here root 5851 5850 0 00:00:00 pts/4 00:00:00/bin/bashroot 5974 5830 5851 0 pts/3 grep [root @ rhel63single ~] # Ps-ef | grep 5850 ----> note here root 5850 1 0? 00:00:00 SCREEN-dmS lei2 ----> note here root 5851 5850 0 00:00:00 pts/4 00:00:00/bin/bash ----> note here root 5883 5810 5850 0 pts/2 screen-r [root @ rhel63single ~] #
8. Check whether the current prompt is in the screen session:
Press ctrl, press a, and then press t. in the lower left corner, the message "22:45:06 Mar 19 rhel63single 0.00 0.00 0.00" is displayed in the format of current time, date, host name, and load.
9. Killing other sessions in the screen
[root@rhel63single ~]# kill -9 5850[root@rhel63single ~]# screen -lsThere is a screen on: 5850.lei2 (Dead ???)Remove dead screens with 'screen -wipe'.1 Socket in /var/run/screen/S-root.[root@rhel63single ~]# screen -wipeThere is a screen on: 5850.lei2 (Removed)1 socket wiped out.No Sockets found in /var/run/screen/S-root.[root@rhel63single ~]# screen -lsNo Sockets found in /var/run/screen/S-root.[root@rhel63single ~]#
Then, a message is displayed for the session to be killed:
[root@rhel63single ~]# Suddenly the Dungeon collapses!! - You die...[root@rhel63single ~]#