Previous Section  < Day Day Up >  Next Section

Hack 92 Use IRC Within screen

figs/moderate.gif figs/hack92.gif

If you're regularly on the move, you need a way to keep track of IRC while away from your computer. Run a console-based IRC client in screen as a simple yet powerful solution.

If you're running a text mode (console) IRC client on a remote system, it can be annoying having to reconnect if your connection drops or if you have to move to another machine. When you reconnect, you will no longer see the messages that were sent when you were last connected.

GNU screen provides a neat solution to this problem. It allows you to disconnect from a terminal session without quitting your running programs. You can then log in and resume the screen session at a later time, giving the appearance that you never disconnected.

screen is provided as a package on most Unix-based systems. If it isn't already installed, install the screen package or download and install it from source at

Starting screen is amazingly simple, yet many people overlook the usefulness of it. At a shell prompt, simply type:

% screen

If you get a startup message, just press Enter. You should then see a shell prompt. This is just like any other shell, but with one difference—every time you press Ctrl-A, it will be intercepted by screen. All of screen's commands are accessed by typing a different letter after this key. screen provides a short summary of the commands if you press Ctrl-A followed by the ? key. These combinations are often abbreviated to the form ^A ?.

Probably the most useful command is the one that lets you "detach" from a screen session. Typing ^A d will detach your session, leaving the programs running inside the screen just as they were. To reattach to the session, just type:

% screen -r

You should now see the screen as you left it. You can also log out completely, and later log back in and reattach. By default, screen will also detach sessions when the terminal is closed, so screen sessions survive network connections dying and closing the terminal window. If for some reason your connection dies and the screen isn't detached, screen -r is not enough to reattach. You will need to run the command screen -r -d to detach and then reattach. Also, if you are running more than one screen, you need to give the pid (process ID) or name of the screen process that you want to reattach to after the -r parameter.

If you are using the BitchX IRC client, detaching is even easier. Simply type /detach to detach your client, then run the scr-bx command to bring your session back again. However, this feature is nowhere near as powerful as screen and won't detach automatically.

screen's other great strength is that it lets you run more than one program inside one terminal window. This makes it easy to leave several programs running and access all of them from another location, even if you are restricted to a very slow connection. This is achieved by supporting virtual windows inside the screen session. You can create a new window by pressing ^A c. Once you have more than one window, you can use ^A n or ^A <space> to go to the next window, and ^A p to go to the previous window. This feature is made even more flexible because screen allows windows to be split. This means you can see more than one window on the screen at a time. To split the screen, type ^A S. This one is case sensitive, so you will need to hold down the Shift key as well. This splits the window into two, and you should see a new blank window in the bottom half of the screen. Pressing ^A <tab> will change to this new window, and you can either change to another existing window by pressing ^A n, or you can create a new window. If you want to get rid of the split windows, ^A Q will hide all the inactive windows.

The screenshot in Figure 14-8 shows screen with a split window, displaying irssi in a channel where system logs are sent to IRC, and the screen manual page in the bottom half.

Figure 14-8. Screen with a split window

If you have played with the split-window feature, you may have noticed you can have a window visible in several split windows at the same time. This is actually a very useful feature because screen allows you to attach to a screen session more than once. This is called multiple display mode, and you can use it to display the same window on multiple terminals, or you can display a different window on different terminals. To use it, simply add the -x option to the reattach command, so it becomes:

% screen -r -x

screen also has support for copy and paste from one window to another. Type ^A [ to start the copy, move the cursor with the arrow keys, and press Enter to start copying; then move the cursor to the end of the text you want to copy and press Enter again. The text that you have copied will be stored in memory until you use ^A ] to paste it. When you are selecting the text, there are some other keys that you can use. For example, pressing / will allow you to search within the text buffer, and Page Up and Page Down will scroll a full screen.

More relevant to IRC is a script that checks that your IRC client is running so you don't even have to manually restart if it crashes or if the system you're running it on is rebooted.

This makes use of the cron facility found on most Unix systems, along with a little bit of Bourne shell scripting.

To edit your user's crontab, run this command:

% crontab -e

You can then create a new line in your crontab:

*/5 * * * * IRC=`screen -ls | grep -v Dead | grep "\\.irc"`;

    if [ "x$IRC" = "x" ]; then screen -dmS irc irssi;fi

This causes the script to be run every five minutes. When it runs, it checks the output of screen -ls for a session called irc. If it doesn't find it, it starts screen in detached mode (with the options -dm) and names the session irc (option -S). screen will run the command irssi once it has started. If you want to use a different IRC client, you could replace the irssi with whatever you use to start your IRC client.

screen also has a command line as well as key shortcuts. You can access the command line via ^A :. For example, to change the title of a window (this is useful when you're using split screens), you would type ^A :title new-title.

If you are paranoid about security, you can password-protect reattaching to your screen by running the password command on the screen command line. You can do this by typing ^A :password and following the prompts. If you want to make this permanent after setting the password, edit the file ~/.screenrc (create it if it doesn't exist) and type password followed by ^A ] (this pastes the contents of the paste buffer). Your line should look something like password NSQuRKGNxIEbw. Whenever someone runs screen -r from now on, they will be prompted for the password. The security provided by this is in addition to that provided by your login password, but it won't deter someone who is determined to get past if they have access to your system account.

There isn't enough room to cover all of screen's features here; however, screen has a very good manual page so man screen will tell you lots more, such as how to remap keys to suit your tastes and how to allow multiple users to share a screen session. With screen, it's easy to run multiple IRC clients and access them from anywhere in the world.

Quick key reference:

^A c

Create window.

^A d


^A n or ^A <space>

Next window.

^A p or ^A <backspace>

Previous window.

^A <number key>

Change to that window number.

^A [ or ^A <escape>

Start a copy.

^A ]

Paste copy buffer.

^A S

Split window.

^A Q

Hide inactive windows.

^A :

Enter a screen command.

To get a quick list of all of screen's key bindings, press ^A? at any time.

David Leadbeater

    Previous Section  < Day Day Up >  Next Section