Thursday, January 31, 2013

Screen: Multitasking in the command line

Multitasking Octave and Python with Screen. (Don't ask why, it's an example)

"Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells." - definition from GNU Screen website.

As the definition says Screen allows you to run multiple processes/shells in one terminal, this is priceless when you are sshing a remote server. However you can use the powers of Screen to do some command line multitask in your everyday life.

I'll cover how I use both Screen and the the Gnome Drop Down Terminal (alternatively Guake can also be used). If you use Guake, you might say: "but I already have tabs in Guake, I don't need Screen". That's as debatable as the topic of Buffers vs Tabs in Vim, but everybody knows that Buffers beat Tabs everyday (OK, that's just my opinion).

Basics of Screen


Install Screen from your distro repository if you haven't already. And let's see what Screen can do.
# Try this command
screen
Notice the gnome-terminal screen now says "screen" in the title.

Some splash screen appears, you press enter and then looks like nothing happened... But something did happen, you are now in a Screen session!
# Try this command on your current window
top
You'll now see your running processes order by CPU usage. Now type: 'C-a c' (press both Ctrl and A, release both keys and then press the C key).

Creating a new window in a Screen session.

You have just created a new window and you watching this second window. The command top is still running in the first window, just that is not visible. You can switch between these two windows using 'C-a n' or 'C-a p'.

Finally, you can "kill" a window using the command 'C-a k'. You'll see a prompt at the bottom, asking if you really want to kill window. Killing a window also terminates the process contained in it.

Killing a window an a Screen session.

There are other commands, you can learn more about them using the help command 'C-a h'. There is another important feature of Screen named attaching/detaching, which is extremely useful for ssh connections, but I won't cover that here as it's not necessary to do the multitasking, as you'll see soon.

How do I know if I'm in a Screen session?


I can't visually tell whether I'm in a Screen session or not.

If you run screen inside a a GUI terminal like the gnome-terminal, is easy to notice that you are in a Screen session because the title changes to "screen". On Guake is also easy to notice, as the tab name changes to "screen" accordingly.

But on the Gnome Drop Down Terminal you've got no way to tell. You can't tell either if you are on an X-less login session. At less not visually, but you can always test if 'C-a h' displays the Screen help.

A better way of telling Screen session apart is adding a status line, this line can also display other important information. Before adding that feature, we need to lean about the screenrc file.

screenrc


As with many other applications, Screen has a rc file that executes certain commands at every startup, you can think of it as a configuration file. We'll work with the .screenrc file found in your home folder, that rc file only works with your current user account.
# Creating/editing the screenrc file
vim ~/.screenrc
To turn off the startup message you can add this line to your .screenrc file:

Screen status


To add a nice status line to Screen, you must append the following lines to your .screenrc file. (Source: Arch Linux Forums)
The Screen status line displays the hostname, a list of windows, the date and the time.

Launching a set of applications on every Screen call


To start Screen with multiple windows and each window containing an application, add the following lines to your .screenrc file.
On Screen the windows are numbered starting from 0. The final command selects which window will be active at startup.

For example, to start Screen with two windows, the first containing octave and the second containing the shell; and start with the shell window active, you'll need the following lines:
Start Screen with Octave and a shell (zsh in this example)

Keybindings


Remember the command we tested at the beggining of this post? Well, we can map them to single keys for faster accessing. The following lines do just that, append them to your .screenrc:
Each keybinding has a description right next to it, for your reference.

Drop-down Multitasker Terminal


As a final step let's integrate Screen to a drop down terminal for the ultimate command line multitasking experience.

Gnome Drop Down Terminal


For the Gnome Drop Down Terminal, the process is straightforward: Simply head to the preferences of the extension and select screen as the custom command to run instead of the shell.

Use Screen instead of the default shell in the Gnome Drop Down Terminal

Gnome Drop Down Terminal + Screen

Guake


You can start Guake with some applications in it at startup as I explained in an old post (I used Octave as an example in that post). But using Screen and the .screenrc file is way simpler!

You'll need to run 'guake -e screen' instead of just guake, and the screenrc file will take care of launching your apps.

The easiest way is to configure Guake as a startup application.

Launch Guake with Screen inside it at startup.

Screened Guake

I hope this post was useful to the heavy users of the command line and also pushes the casual users to get into it.

2 comments:

  1. This is very informative posting, it will helpful for us. Thanks for sharing it with us.

    For more related information you can also visit here: www.emblogic.com

    ReplyDelete
  2. Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much

    Installment loans
    Payday loans
    Title loans
    Cash Advances

    ReplyDelete