Search This Blog

Saturday, August 16, 2014

Unix Command: screen

I guess you all know this: you are connected to your server with SSH and in the middle of compiling some software (e.g. a new kernel) or doing some other task, which takes lots of time, and suddenly your connection drops for some reason, and you lose your labour. This can be very annoying, but fortunately there is a small utility called screen which lets you reattach to a previous session so that you can finish your task. This short tutorial shows how to use screen for just this purpose.

Benefits of Screen Command:

1)  Remote terminal session management (detaching or sharing terminal sessions)
2)  unlimited windows
3)  scroll back buffer
4)  copy/paste between windows
5)  notification of either activity or inactivity in a window
6)  split terminal (horizontally and vertically) into multiple regions
7)  locking other users out of terminal


Starting Screen
Screen is started from the command line just like any other command:

[root@office ~]# screen
You are now inside of a window within screen. This functions just like a normal shell except for a few special characters.

Command: “Ctrl-a”
Screen uses the command “Ctrl-a” that is the control key and a lowercase “a” as a signal to send commands to screen instead of the shell.
For example, “Ctrl-a” then “?”. You should now have the screen help page.

  Screen key bindings, page 1 of 1.

                         Command key:  ^A   Literal ^A:  a

break       ^B b       fit         F          lastmsg     ^M m       number      N          redisplay   ^L l       suspend     ^Z z       writebuf    >        
clear       C          flow        ^F f       license     ,          only        Q          remove      X          time        ^T t       xoff        ^S s     
colon       :          focus       ^I         lockscreen  ^X x       other       ^A         removebuf   =          title       A          xon         ^Q q     
copy        ^[ [       hardcopy    h          log         H          pow_break   B          reset       Z          vbell       ^G       
detach      ^D d       help        ?          login       L          pow_detach  D          screen      ^C c       version     v        
digraph     ^V         history     { }        meta        a          prev        ^H ^P p ^? select      '          width       W        
displays    *          info        i          monitor     M          quit        \          silence     _          windows     ^W w     
dumptermcap .          kill        K k        next        ^@ ^N sp n readbuf     <          split       S          wrap        ^R r     

^]  paste .
"   windowlist -b
-   select -
0   select 0
1   select 1
2   select 2
3   select 3
4   select 4
5   select 5
6   select 6
7   select 7
8   select 8
9   select 9
I   login on
O   login off
]   paste .


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.

Creating Windows

Command: “Ctrl-a” “c”.
To create a new window, you just use “Ctrl-a” “c”.
This will create a new window for you with your default prompt.  Your old window is still active.
For example, I can be running “top” or “sleep 1000” 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.)
Start top
Load averages: 0.04, 0.04, 0.05
273 processes: 203 sleeping, 70 running
Cpu states:
CPU   LOAD   USER   NICE    SYS   IDLE  BLOCK  SWAIT   INTR   SSYS
 0    0.04   0.4%   0.0%   0.2%  99.4%   0.0%   0.0%   0.0%   0.0%
 1    0.03   0.0%   0.0%   1.2%  98.8%   0.0%   0.0%   0.0%   0.0%
 2    0.05   0.0%   0.0%   0.0% 100.0%   0.0%   0.0%   0.0%   0.0%
 3    0.03   0.6%   0.0%   2.7%  96.7%   0.0%   0.0%   0.0%   0.0%
---   ----  -----  -----  -----  -----  -----  -----  -----  -----
avg   0.04   0.2%   0.0%   1.0%  98.8%   0.0%   0.0%   0.0%   0.0%

Now open a new window with "Ctrl-a” “c”
Your top window is still running you just have to switch back to it.

Switching Between Windows

Command: “Ctrl-a” “n”
Screen allows you to move forward and back. In the example above, you could use “Ctrl-a “n” to get back to top. This command switches you to the next window.
The windows work like a roundabout and will loop back around to your first window.
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 until you kill that window.
You could also use:
# “Ctrl-a” “Ctrl-a”   # Toggle / switch between the current and previous window
# “Ctrl-a” “0-9”      #Go to a window numbered 0 9, “Ctrl-a” “w” to see number
# “Ctrl-a” “Spacebar” #Go to next Terminal
# “Ctrl-a” “Backspace”      #Come back to previous terminal

 

 

Detaching From Screen

Command: "Ctrl-a" "d"
Detaching is the most powerful part of screen.  Screen allows you to detach from a window and reattach later.
If your network connection fails, screen will automatically detach your session!
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 or during something, which you forget to keep in nohup.

bash-4.1$
[detached]

Reattach to Screen

If your connection drops or you have detached from a screen, you can re-attach by just running:

[jeffh@office ~]$ screen -r



This will re-attach to your screen.
However, if you have multiple screens you may get this:

ibhagat@office#screen -r
There are several suitable screens on:
        16076.pts-26.office   (Attached)
        2917.pts-142.office   (Detached)
        16873.pts-16.office   (Detached)
        4945.pts-13.office    (Attached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

If you get this, just specify the screen you want.

[jeffh@office ~]$ screen -r  16873.pts-16.office

Logging Your Screen Output

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.
A file named “screenlog.0” will be created in your home directory.
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 many changes. If something goes awry, you can look back through your logs.

Getting Alerts

Screen can monitor a window for activity or inactivity. This is great if you are downloading large files, compiling, or waiting for output.
If you are waiting for output from a long running program, you can use “Ctrl-a” “M” to look for activity. Screen will then flash an alert at the bottom of the page when output is registered on that screen.

I use this when running a command that takes a long time to return data. I can just fire up the command, switch to another window and not have to keep switching back to check the status.

You can also monitor for inactivity. Why use this?

If you are downloading a large file or compiling a program, you can be notified when there is no more output. This is a great signal to when that job is done. To monitor for silence or no output use “Ctrl-A” “_”.

Locking Your Screen Session

If you need to step away from your computer for a minute, you can lock your screen session using "Ctrl-a" "x".  This will require a password to access the session again.

bash-4.1$            
Screen used by Indrajit Bhagat <ibhagat>.
Password:

Stopping Screen

When you are done with your work, I recommend you stop the session instead of saving it for later.  To stop screen you can usually just type exit from your shell. This will close that screen window.  You have to close all screen windows to terminate the session.
You should get a message about screen being terminated once you close all windows.

[screen is terminating]

Alternatively, you can use "Ctrl-a" "k".  You should get a message if you want to kill the screen.

Quit Screen:

Used “Ctrl-a Ctrl+\”

Setting the title of the Screen:
To start a new screen with a specific name, you could provide the name as below:

# screen –t foo
Num Name                                      Flags

  0 bash                                      $
  1 bash                                      $
  2 bash                                      $
  3 foo                                       $

To rename or to give name to existing screen, use the below command:

# “Ctrl-a :title foo”

Listing Screen Window:
# “Ctrl-a” “w”  #List all windows (the current window is marked with "*")

0$bash  1$ bash  2-$ bash  3*$ foo

Also
#”Ctrl+a” “ ” ” #List all window


Num Name                                      Flags

  0 bash                                      $
  1 bash                                      $
  2 bash                                      $
  3 foo                                       $
Using the screen as terminal multiplexer
# “Ctrl-a” “i”  #To Split the svreen vertically
# “Ctrl-a” “S”   #To Split the screen horizontally
# “Ctrl-a” “Q”   #To unsplit
Note: After splitting, you need to go into the new region and start a new session via ctrla then cbefore you can use that area.

To copy a block

To get into copy mode, press Ctrl-a [ .
To move the cursor, press the h, j, k, and l (the letter l) keys. The 0 (the number 0) or ^ (the caret) moves to the start of the line and $ (the dollar sign) moves to the end of the line. Ctrl-b scrolls the cursor back one page and Ctrl-f scrolls forward one page. To set the left and right margins of copy, press c and C (Shift-c). The Spacebar starts selecting the text and ends selecting the text. To abort copy mode, press Ctrl-g.

To paste a block

To paste the copied text to the current window (as many times as you want), press Ctrl-a ].

Unix Command: xargs

Things you (probably) didn’t know about xargs

If you have spent any amount of time at a UNIX command line, you have probably already seen xargs. In case you have not, xargs is a command used to execute commands based on arguments from standard input.

Common use cases

I often see xargs used in combination with find in order to do something with the list of files returned by find.

Contrived examples warning: I needed something simple examples that would not detract from the topic. This is the best I could do given the time I had.

Recursively find all Python files and count the number of lines

# find . -name '*.py' | xargs wc –l

Recursively find all Emacs backup files and remove them

# find . -name '*~' | xargs rm

Recursively find all Python files and search them for the word ‘import’

# find . -name '*.py' | xargs grep ‘import’

Handling files or folders with spaces in the name
One problem with the above examples is that it does not correctly handle files or directories with a space in the name. This is because xargs by default will split on any white-space character. A quick solution to this is to tell find to delimit results with NUL (\0) characters (by supplying -print0 to find), and to tell xargs to split the input on NUL characters as well (-0).
Remove backup files recursively even if they contain spaces

# find . -name '*~' –print0 | xargs -0 rmg
Security note: filenames can often contain more than just spaces.
Placement of the arguments
In the examples above, xargs reads all non-white-space elements from standard input and concatenates them into the given command line before executing it. This alone is very useful in many circumstances. Sometimes however you might want to insert the arguments into the middle of a command. The -I flag to xargs takes a string that will be replaced with the supplied input before the command is executed. A common choice is %.

Move all backup files somewhere else

# find . -name '*~' –print0 | xargs -0 –I % cp % ~/backups

Maximum command length
Sometimes the list of arguments piped to xargs would cause the resulting command line to exceed the maximum length allowed by the system. You can find this limit with
getconf ARG_MAX
In order to avoid hitting the system limit, xargs has its own limit to the maximum length of the resulting command. If the supplied arguments would cause the invoked command to exceed this built in limit, xargs will split the input and invoke the command repeatedly. This limit defaults to 4096, which can be significantly lower than ARG_MAX on modern systems. You can override xargs’s limit with the -s flag. This will be particularly important when you are dealing with a large source tree.
Operating on subset of arguments at a time
You might be dealing with commands that can only accept 1 or maybe 2 arguments at a time. For example, the diff command operates on two files at a time. The -n flag to xargs specifies how many arguments at a time to supply to the given command. The command will be invoked repeatedly until all input is exhausted. Note that on the last invocation you might get less than the desired number of arguments if there is insufficient input. Let’s simply use xargs to break up the input into 2 arguments per line

$ echo {0..9} | xargs -n 2

0 1
2 3
4 5
6 7
8 9
In addition to running based on a specified number of arguments at time you can also invoke a command for each line of input at a time with -L 1. You can of course use an arbitrary number of lines a time, but 1 is most common. Here is how you might diff every git commit against its parent.

# git log --format="%H %P" | xargs -L 1 git diff
Executing commands in parallel
You might be using xargs to invoke a compute intensive command for every line of input. Wouldn’t it be nice if xargs allowed you to take advantage of the multiple cores in your machine? That is what -P is for. It allows xargs to invoke the specified command multiple times in parallel. You might use this for example to run multiple ffmpeg encodes in parallel. However, I am just going to show you yet another contrived example.
Parallel sleep
$ time echo {1..5} | xargs -n 1 -P 5 sleep

real    0m5.013s
user    0m0.003s
sys     0m0.014s
Sequential sleep
$ time echo {1..5} | xargs -n 1 sleep

real    0m15.022s
user    0m0.004s
sys     0m0.015s

Examples

Example 1: Copy large number of files to another folder.
Sometimes we required to copy a long list of files, In that case cp command failed with error “Argument list too long”. We can use xargs to do that task.

# find /home/linuxman/public_html/tecadmin.net/ -type f | xargs -n1 -i cp {} /var/www/backup/
Example 2: Delete multiple files from a folder.
Sometimes we required to delete a large number of files from a folder. Below example will delete all .log files from /var/log directory.

# find /var/www/tmp/ -type f | xargs rm –f

Above command will fail to remove files with spaces in named. Say File name is “Indrajit Bhagat”, to handle spaces in xargs command try below command.

# find /var/www/tmp/ -type f -print0 | xargs -0 rm -f
Example 3: Count number of lines in multiple files.
Below example will count number of lines for each .txt file in /opt directory and its subdirectory

# find /opt -name "*.txt" | xargs wc –l
To handle files having spaces in their name, use following command.

# find /opt/ -name "*.log" -print0 | xargs -0 wc -l
Example 4: Make a backup of all configuration files.
If you want to make a backup of all configuration files (extension .conf) in your system, use below command.

# find / -name "*.conf" |  xargs tar czf  config.tar.gz
# ls -l config.tar.gz
-rw-r--r--. 1 root root 193310 Apr  1 13:26 config.tar.gz
Example 5: Use custom delimiter with xargs.
We can have also use custom delimiter with xargs command; by default, it uses space and new line as delimiter. Use -d parameter to define delimiter.

# echo "1,2,3,4,5" | xargs -d, echo
Output:

1 2 3 4 5
Example 6: Show output in separate line with xargs.
In example 5 output is showing in single line, we can also specify to show each output in separate line.

# echo "1,2,3,4,5" | xargs -d, -L 1 echo
Output:

1
2
3
4
5
Example 7: Handling blank space in filenames or path.
To handle spaces in names use -print0 with find command and -0 with xargs command as parameter.

#  find /tmp -print0 | xargs -0 -L 1 echo