Thursday, May 10, 2012

screen man page


screen Multiplex a physical terminal between several processes (typically interactive shells). Syntax: Start a screen session: screen [ -options ] [ cmd [args] ] Resume a detached screen session: screen -r [[pid.]tty[.host]] screen -r sessionowner/[[pid.]tty[.host]] Options: -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. -D (-r) Detach and logout remote (and reattach here). -D -RR Do whatever is needed to Reattach a screen session. -d -m Start in "detached" mode. Useful for system startup scripts. -D -m Start in "detached" mode, & don't fork a new process. -list List our SockDir and do nothing else (-ls) -r Reattach to a detached screen process. -R Reattach if possible, otherwise start a new session. -t title Set title. (window's name). -U Tell screen to use UTF-8 encoding. -x Attach to a not detached screen. (Multi display mode). -X Execute cmd as a screen command in the specified session. Interactive commands (default key bindings): Control-a ? Display brief help Control-a " List all windows for selection Control-a ' Prompt for a window name or number to switch to. Control-a 0 Select window 0 Control-a 1 Select window 1 ... ... Control-a 9 Select window 9 Control-a A Accept a title name for the current window. Control-a b Send a break to window Control-a c Create new window running a shell Control-a C Clear the screen Control-a d Detach screen from this terminal. Control-a D D Detach and logout. Control-a f Toggle flow on, off or auto. Control-a F Resize the window to the current region size. Control-a h Write a hardcopy of the current window to file "hardcopy.n" Control-a H Begin/end logging of the current window to file "screenlog.n" Control-a i Show info about this window. Control-a k Kill (Destroy) the current window. Control-a l Fully refresh current window Control-a M Monitor the current window for activity {toggle on/off} Control-a n Switch to the Next window Control-a N Show the Number and Title of window Control-a p Switch to the Previous window Control-a q Send a control-q to the current window(xon) Control-a Q Delete all regions but the current one.(only) Control-a r Toggle the current window's line-wrap setting(wrap) Control-a s Send a control-s to the current window(xoff) Control-a w Show a list of windows (windows) Control-a x Lock this terminal (lockscreen) Control-a X Kill the current region(remove) Control-a Z Reset the virtual terminal to its "power-on" values Control-a Control-\ Kill all windows and terminate screen(quit) Control-a : Enter command line mode(colon) Control-a [ Enter copy/scrollback mode(copy) Control-a ] Write the contents of the paste buffer to stdin(paste) Control-a _ Monitor the current window for inactivity {toggle on/off} Control-a * Show a listing of all currently attached displays. When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would. Then, at any time, you can: Create new (full-screen) windows with other programs in them (including more shells) Kill existing windows View a list of windows Switch between windows - all windows run their programs completely independent of each other. Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user's terminal. The interactive commands above assume the default key bindings. You can modify screen’s settings by creating a ~/.screenrc file in your home directory. This can change the default keystrokes, bind function keys F11, F12 or even set a load of programs/windows to run as soon as you start screen. Attaching and Detaching Once you have screen running, switch to any of the running windows and type Control-a d. this will detach screen from this terminal. Now, go to a different machine, open a shell, ssh to the machine running screen (the one you just detached from), and type: % screen -r This will reattach to the session. Just like magic, your session is back up and running, just like you never left it. Exiting screen completely Screen will exit automatically when all of its windows have been killed. Close whatever program is running or type `Exit ' to exit the shell, and the window that contained it will be killed by screen. (If this window was in the foreground, the display will switch to the previous window) When none are left, screen exits. This page is just a summary of the options available, type man screen for more. "Growing old is mandatory, but growing up is optional" - Motto of the Silver Screen Saddle Pals ======================================== Using screen (virtual terminal multiplexer) What is screen? “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).” (from man page) In every-day use you just type $ screen and it seems that nothing happens. :) In fact a program is run that manages multiple bash processes and allows doing cool stuff in console(s) without having to run X. Basics Ctrl+a is a basic command that allows you to control screen. After you've pressed Ctrl+a your input is not being sent to current console, but to the screen itself. That's where you issue one-letter commands. (and more, but let's not get you confused) basic commands Ctrl+a c - create new console Ctrl+a n - switch to next console (it loops) Ctrl+a p - switch to previous console (it loops) Ctrl+a a - send Ctrl+a combination to the program, I didn't want to call screen Ctrl+a k - kill current console (asks to confirm)(ends screen when last console is killed) Ctrl+a d - detach - exits screen but leaves consoles and theri processes running For jornada it might be comfortable to bind winkey to do Ctrl+a. I'll expand the tutorial when I find the best way to do it. Copy and paste [[awesome]] You like using links browser in text mode, but you hate having to start X or use wget to copy some text from a page? Screen is the answer! Ctrl+a [[ - switch to copy mode - then use arrows to move cursor - press Enter to start selection - move cursor to the end - press Enter again Now switch to another console with your awesome script being developed in an editor or just jump to a textfield on a page… Ctrl+a ]] - pastes the copied text Useful for transporting data between two separate instances of screen or for using copied text as an input file for programs: Ctrl+a > - writes copied buffer to a file given in config (/tmp/screen-exchange by default, but don't go looking through options - it'll tell you where it writes) Ctrl+a < - loads the file to buffer Splitting screen You can split your screen and display two (or more, but…) consoles at the same time. Screen avaliable in jlime repos supports only horizontal splitting, so I won't mention the vertical splitting keys. Ctrl+a S - does the split (S is Shift+s and it has to be capital S) - new screen region is created - you probably want to press //Ctrl+a c// to start a new console in there Ctrl+a Tab - switches between visible splits Ctrl+a :resize (Enter) - prompts for number of lines and resizes current split Ctrl+a X - removes current split Monitoring activity If you work in one console and running something in another you might want to monitor it. Ctrl+a M - starts monitoring current console for changes (Will inform you when a program outputs something new. Good for IM clients) Ctrl+a _ - start monitoring current console for silence (Will inform you when a program stops outputting new stuff. Good for compiling or downloading with wget) Sessions and attaching Mostly useful when working on a remote machine with screen, but you might want to know this. When you loose a connection or leave screen with Ctrl+a d your processes stay there. You might want to get to them again I suppose :) $ screen -ls There is a screen on: 21209.pts-0.computername (Detached) 1 Socket in /var/run/screen/S-username. To get back to the running screen type $ screen -r 21209 21209 in both examples above is a PID of the screen process. ===================================== http://snipplr.wordpress.com/2012/04/07/my-screenrc-with-custom-hardstatus-and-easy-switching-between-screens/ screen setup with custom hard status and dialog menu Posted on April 7, 2012 The following set of scripts produce a nice screen setup along with a dialog menu for the most common tasks. This is how it looks like And this is how it’s done. Please see inline comments for details. 01 #!/bin/bash 02 ## use shift-left/right to switch between windows 03 ## use ctrl-n to add new shellwindow 04 ## use shift-PgUp/PgDown for scrolling history hit esc to stop 05 ## use ctrl-a-k for killing a window 06 ## mark area with space, end marking with space, paste with ctrl-a-] 07 setenv TERM xterm 08 09 #kill startup messes 10 startup_message off 11 12 # detach on hangup 13 autodetach on 14 15 # define a bigger scrollback, default is 100 lines 16 defscrollback 4096 17 18 # shell 19 shell -bash 20 21 #make scrollbar work 22 termcapinfo xterm ti@:te@ 23 24 defmonitor on # turn monitoring on 25 activity "%" # tell me when stuff happens! 26 27 # Make shift-PgUp and shift-PgDn work like they do in xterm. (Note that this 28 # requires xterm to be configured to pass those keys through, and not try to 29 # act on them itself.) 30 bindkey "^[O2A" eval "copy" "stuff ^b" 31 bindkey -m "^[O2A" stuff ^u 32 bindkey -m "^[O2B" stuff ^d 33 34 bindkey "^[O2D" prev 35 bindkey "^[O2C" next 36 37 # ctrl-N for new window 38 bindkey "^N" screen 39 40 # ctrl-a-b to copy selection to osx-clipboard 41 #bind b eval "writebuf" "exec sh -c 'pbcopy < /tmp/screen-exchange'" 42 43 # Window numbering starts at 1, not 0. 44 bind c screen 1 45 bind 0 select 10 46 47 # Run everything in UTF-8. 48 defutf8 on 49 # If a window goes unresponsive, don't block the whole session waiting for it. 50 nonblock on 51 52 # An alternative hardstatus to display a bar at the bottom listing the 53 # windownames and highlighting the current windowname in blue. (This is only 54 # enabled if there is no hardstatus setting for your terminal) 55 # see http://www.debian-administration.org/articles/560 56 hardstatus on 57 hardstatus alwayslastline 58 hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a " 59 60 screen -t selector sh screen-selector.sh I use this via screen -c selector-screenrc. The helper script for initializing a screen session looks like this and would either connect to an existing one or create a new one. 1 #!/bin/bash 2 3 screenpid=`ps x|grep -E '(.*)SCREEN(.*)selector-screenrc\$'|awk '{print \$1}'|head -1` 4 if [ $screenpid -gt 0 ]; then 5 screen -x $screenpid -c selector-screenrc 6 else 7 screen -c selector-screenrc 8 fi The screen-selector.sh file is a bonus and provides a dialog menu for frequently used commands. It looks like this 01 #!/bin/bash 02 03 while [ 1 ]; do 04 05 select=`/opt/local/bin/dialog --stdout --menu "select task" 20 60 13 \`cat tasks | tr '\040' '_' | tr '\012' '\040'\`` 06 07 echo "selected $select" 08 09 if [ ! -z $select ]; then 10 11 echo $select 12 13 command=`grep $select tasks|awk 'BEGIN{FS=OFS="\t"}{print \$2}'|tr -d "\""` 14 15 echo "executing command \"$command\"" 16 17 $command 18 # sleep 2 19 else 20 exit 0 21 fi 22 23 done The “tasks” file holds a simple list of tasks/commands. See the example below. 1 su "screen -t rootshell sudo su -" 2 newshell "screen -t shell"

No comments:

Post a Comment