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.
If you are using debian just type
apt-get install screen
and you are ready to go
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:
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:
What we will do is start the collecting process and then detach the window. This is pretty easy:
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:
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?
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.