//
archives

Productivity

This category contains 1 post

How to use linux screen

If you are using linux you have probably sumbled across screen. It is a great tool to run some processes that take a long time and detach them from the console.

In our example we are collecting tweets, since this process can go on for a while or actually forever unless we stop it, we will use screen to start our collecting program and then let it continue for some days. Normally closing your console also closes your program unless you don’t deamonize it. Screen is an easy alternative to do that.

Step 1.

If you are using debian just type

apt-get install screen 

and you are ready to go

Step 2:

To start using screen simply type screen and you will be greeted with a welcome screen. Now you are inside screen and everything looks the same apart that you can do a few cool tricks:

Shortcuts

The following are some of the most used shortcuts that lets you
navigate through your screen environment. Note that unless modified by
your .screenrc, by default every screen shortcut is preceded by Ctrl+a.
Note that these shortcuts are case-sensitive.

  • 0 through 9 – Switches between windows
  • Ctrl+n – Switches to the next available window
  • Backspace – Switches to the previous available
  • Ctrl+a – Switches back to the last window you were on
  • A – Changes window session name
  • K – Kills a window session
  • c – Creates a new window
  • [ – Then use arrows to scroll up and down terminal

Find out about more shortcuts in Screen’s man pages. In your terminal, run: man screen.

You will find great tutorials on the web on screen like this or this. They will explain screen in much more detail to you.

Step 3:

What we will do is start the collecting process and then detach the window. This is pretty easy:

Type

ruby collect_tweets.rb 

to start collecting the tweets, you will see them popping up in your console.

Now to detach the window simply press

ctrl+ a + d

and you are back in your console. Notice that screen is still running your tweet collection.

To go back to screen and see what it is doing simply type:

screen -ls

It will provide you with the instances of screen that you are running now. You should find one entry. To connect to that session simply type:

screen -r and the pid number for example

screen -r 27699

And voila you are back in watching your ruby program collecting those tweets.

Sweet isn’t it?

Step 4:

Extra goodies:

You can have multiple consoles in screen to create a new one simply press

ctrl + a + c

and you have a new console inside screen. To cycle between those consoles press

ctrl + a + n 

to flip to the next window.

Now you are able to start those collecting processes and safely disconnect from your box while it is still preforming the collection process.

In the next tutorial I will show you how to monitor what is going on from the browser.

Cheers
Thomas