Tuesday, February 06, 2007

Supervising terminal activity remotely

Suppose you started a process in terminal of your PC and then logged into your PC via ssh from another PC. Now u want to see what's going on in the terminal in the first PC. There are mainly two different approaches that I've come accross. Here's the first solution. This one is fairly simple:

In the main PC I run:

$mkfifo foo; script -f foo

In the second PC, I log in via ssh and then run in the terminal:

$cat foo

Now whatever I run in the 1st PC's terminal, it gets showed (including all the verbose output) in the second PC.

This simple solution has a minor drawback, second PC's terminal becomes read-only, You don't get any kind of interactivity from the second shell. To solve this problem we'll take help of another tool, GNU 'screen'.

#apt-get install screen

$vi ~/.screenrc

Add these lines:
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
Now comes the most important part:

In the main PC I open a shell:
$screen

Now I log in via ssh from the remote PC and run this in shell:
$screen -x

Now whatever I run in the first PC, I get another output in the second terminal and both the terminal's are interactive.

Now you can ask, what if I want to close either of the screens and keep the other one active? Just press Ctrl + a, d in any screen. The screen gets detached.

If you want to resume an already detached screen:
$screen -r

Some tips:
  • Screen can also manage multiple terminal sessions in it. To create new tab, Ctrl + a, c and then you can cruise between the tabs by pressing Ctrl + a, Ctrl + a repeatedly.
  • Ctrl + a, " shows you the list of available tabs
  • Ctrl + a, A gives you a promt to change the name of current tab
  • Ctrl + a, k closes the current tab
  • Ctrl + a, S splits the currnt window into two separete workspaces, You can browse between them by Ctrl + a, TAB
  • You can also run 'screen' in a nested manner. Cool huh!
You can find more info here:
http://www.kuro5hin.org/story/2004/3/9/16838/14935

No comments:

Back to Blogging

After a failed attempt last year to get back to blogging, I'm trying it again this year. I really wanted to get back, but got busy will...