AdSense Mobile Ad

Sunday, November 16, 2008

Back to CLIs: using a physical terminal multiplexer on Solaris 10

How many times did a remote terminal session unexpectedly close and you lost some work? How many times did you fell that a nohup was insufficient but it was the only option left to you? In many situations GNU Screen was very useful to me.

What GNU Screen is not.
It's not a full fledged solution to open, detach and reattach graphical session. It's not VNC or similar software.

What GNU Screen is.
Technically it's a screen multiplexer. This means that with screen you can literally multiplex physical terminals, launch processes inside them, detach from sessions and reattach from whichever machine. Screen is pretty powerful and I suggest you to read the official documentation. Nevertheless, a quick crash course may be useful.

Installing GNU's screen (a screen multiplexer).
This applies only to Solaris 10. Screen is also included in some Solaris Express and OpenSolaris versions. As usual, I'll install it from Blastwave, which will take care of all dependencies (which in this case it's just CSWcommon which will be installed if you'd already configured Blastwave on your machine). Here's the package and here are commands. First, let's refresh the software catalog from out preferred mirror:
# /opt/csw/bin/pkg-get -U
Then, let's install screen:
# /opt/csw/bin/pkg-get -i screen
How to start a screen session
It's easy, just typing screen:
$ screen
Screen will present you a welcome screen and hitting space or enter will bring you to your new screen session. Since now on, every process will be running inside screen's multiplexed physical terminal.

Exiting a session

This is easy too. Just exit screen as you'll exit your favorite shell:
$ exit
[screen is terminating]
Detaching from a session
If you want to detach from the current session and leave programs running, you can just hit CTRL+A,D and you'll detach the screen you're using:
$ [hit CTRL+A,D]
Listing the open sessions
Sessions are maintained on a per-user base. Just log in with your user and type:
$ screen -ls
There are screens on:
3492.console.PCS30575 (Dead ???)
4544.console.PCS30575 (Detached)
6028.console.PCS30575 (Dead ???)
Remove dead screens with 'screen -wipe'.
3 Sockets in /tmp/uscreens/S-A144286.
Screen's telling you that you have 3 open sessions: one is a detached screen (4544.console.PCS30575) and another two which are probably dead session you couldn't wake up any more. If you want to wipe them, just use the wipe option (note that the using the PID is sufficient because we're connecting from and to the same machines we detached screens from):
$ screen -wipe 3492
There is a screen on:
3492.console.PCS30575 (Removed)
1 socket wiped out.
No Sockets found in /tmp/uscreens/S-A144286.
You can know wipe the other one. To reattach to the 4544 session:
$ screen -r 4544
If you check screen documentation you'll see plenty of options which allow you to detach and reattach with a single command (if you're connecting from another client and you left a session open on another machine), etc.

Launching a command on a new screen session and detach it immediately
This is one of those I use most. If you need to launch a program and you want to launch it inside a new screen session without even attaching to it, you can use the following syntax:
$ screen -m -d [your-command]
This will create a new session, launch your-command inside it and leave it detached.

No comments: