Advertisement
chotoipho

BASH: Using 'screen' command

Jan 10th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. Using Screen
  2.  
  3. Screen is started from the command line just like any other command:
  4.  
  5. [admin@gigan admin]$ screen
  6.  
  7. You may or may not get a text message about screen. If you do not, then you probably think nothing has happened, but it has. You are now inside of a window within screen. This functions just like a normal shell except for a few special characters. Screen uses the command “Ctrl-A” as a signal to send commands to screen instead of the shell. To get help, just use “Ctrl-A” then “?”. You should now have the screen help page.
  8. Screen key bindings, page 1 of 2.
  9.  
  10. Command key: ^A Literal ^A: a
  11.  
  12. break ^B b lockscreen ^X x reset Z
  13. clear C log H screen ^C c
  14. colon : login L select " '
  15. copy ^[ [ meta a silence _
  16. detach ^D d monitor M split S
  17. digraph ^V next ^@ ^N sp n suspend ^Z z
  18. displays * number N time ^T t
  19. fit F only Q title A
  20. flow ^F f other ^A vbell ^G
  21. focus ^I pow_break B version v
  22. help ? pow_detach D width W
  23. history prev ^P p ^? windows ^W w
  24. info i readbuf < wrap ^R r
  25. kill K redisplay ^L l writebuf >
  26. lastmsg ^M m remove X xoff ^S s
  27. license , removebuf = xon ^Q q
  28. [Press Space for next page; Return to end.]
  29.  
  30. Key bindings are the commands the screen accepts after you hit “Ctrl-A”. You can reconfigure these keys to your liking using a .screenrc file, but I just use the defaults. The power of screen will become obvious, especially if you need to bounce around to different file system locations and leave processes running. For example, when I go in to clean out wasted disk space, I can remove files in one screen while hunting for other files in another.
  31. Multiple Windows
  32.  
  33. Screen, like many windows managers, can support multiple windows. This is very useful for doing many tasks at the same time without opening new sessions. As a systems manager, I often have four or five SSH sessions going at the same time. In each of the shell, I may be running two or three tasks. Without screen, that would require 15 SSH sessions, logins, windows, etc. With screen, each system gets its own single session and I use screen to manage different tasks on that system.
  34.  
  35. Another trick I use is when I am editing configuration files. For example, if I want to harden SSH, I can cat out the configuration file in one window and then edit it in another.
  36.  
  37. To open a new window, you just use “Ctrl-A” “c”. This will create a new window for you with your default prompt. For example, I can be running top and then open a new window to do other things. Top stays running! It is still there. To try this for yourself, start up screen and then run top. (Note: I have truncated some screens to save space.)
  38.  
  39. Start top
  40.  
  41. Mem: 506028K av, 500596K used, 5432K free,
  42. 0K shrd, 11752K buff
  43. Swap: 1020116K av, 53320K used, 966796K free
  44. 393660K cached
  45.  
  46. PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
  47. 6538 root 25 0 1892 1892 596 R 49.1 0.3
  48. 6614 root 16 0 1544 1544 668 S 28.3 0.3
  49. 7198 admin 15 0 1108 1104 828 R 5.6 0.2
  50.  
  51. Now open a new window with “Ctrl-A” “c”
  52.  
  53. [admin@ensim admin]$
  54.  
  55. To get back to top, use “Ctrl-A “n”
  56.  
  57. Mem: 506028K av, 500588K used, 5440K free,
  58. 0K shrd, 11960K buff
  59. Swap: 1020116K av, 53320K used, 966796K free
  60. 392220K cached
  61.  
  62. PID USER PRI NI SIZE RSS SHARE STAT %CPU %ME
  63. 6538 root 25 0 1892 1892 596 R 48.3 0.3
  64. 6614 root 15 0 1544 1544 668 S 30.7 0.3
  65.  
  66. You can create several windows and toggle through them with “Ctrl-A” “n” for the next window or “Ctrl-A” “p” for the previous window. Each process will keep running while your work elsewhere.
  67. Leaving Screen
  68.  
  69. There are two ways to get out of screen. The first is just like logging out of a shell. You kill the window with “Ctrl-A” “K” or “exit” will work on some systems. This will kill the current windows. If you have other windows, you will drop into one of those. If this is the last window, then you will exit screen.
  70.  
  71. The second way to leave screen is to detach from a windows. This method leaves the process running and simple closes the window. If you have really long processes, you need to close your SSH program, you can detach from the window using “Ctrl-A” “d”. This will drop you into your shell. All screen windows are still there and you can re-attach to them later. This is great when you are using rsync for server migration.
  72. Attaching to Sessions
  73.  
  74. So you are using screen now and compiling that program. It is taking forever and suddenly your connection drops. Don’t worry screen will keep the compilation going. Login to your system and use the screen listing tool to see what sessions are running:
  75.  
  76. [root@gigan root]# screen -ls
  77. There are screens on:
  78. 31619.ttyp2.gigan (Detached)
  79. 4731.ttyp2.gigan (Detached)
  80. 2 Sockets in /tmp/screens/S-root.
  81.  
  82. Here you see I have two different screen sessions. To re-attach to a session, use the re-attach command:
  83.  
  84. [root@gigan root]#screen -r 31619.ttyp2.gigan
  85.  
  86. Just use screen with the -r flag and the session name. You are now re-attached to the screen. A nice thing about this, is you can re-attach from anywhere. If you are at work or a clients office, you can use screen to start a job and then logout. When you get back to your office or home, you can login and get back to work.
  87. Screen Logging
  88.  
  89. As a consultant, I find it important to keep track of what I do to someone’s server. Fortunately, screen makes this easy. Using “Ctrl-A” “H”, creates a running log of the session. Screen will keep appending data to the file through multiple sessions. Using the log function is very useful for capturing what you have done, especially if you are making a lot of changes. If something goes awry, you can look back through your logs.
  90. Linux Screen Tips
  91.  
  92. Just wanted to mention to other cool tricks you can do with screen. Screen can monitor a window for activity or lack thereof. This is great if you are downloading large files, compiling, or watching for output. If you are downloading something or compiling, you can watch for silence. To start the monitor, go to the screen you want to monitor and use “Ctrl-A” “M” to look for activity or “Ctrl-A” “_” to monitor for silence. Then open or switch to a new window. When the monitor detects activity or silence, you will get an alert at the bottom with the window number. To quickly go to that window, use “Ctrl-A” “ (thats a quote mark, ctrl-a then a “). After you do this, just type in the number of the window and enter. To stop monitoring, go to that window and undo the monitor with the same command. For example, to stop monitoring for activity you would use “Ctrl-A” “M” again.
  93.  
  94. Screen is a great tool and I use it daily for tasking ranging from hardening ssh against brute force attacks to deploying Nginx web server for a client.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement