Automated Screen session with meinscreen

On my usual computer I always have an open Screen session with a few fixed windows, each permanently running a frequently used program (Mutt with e-mail, Gnus with news channels…). Thus, when I happen to switch to another computer, I can connect to the first one via SSH, attach the previous Screen session there and then go on using those programs where I left them (with no need for web services or synchronization between hosts). I've even set up some trick to allow me to edit files and browse web pages from the Screen session on the local X display or Firefox session, but that's another story that I'll tell in another occasion. ;)

The problem is that each time I started a new session on my usual computer I had to launch a new Screen session, create windows, name them (I sometimes have up to seven open windows) and start each program on the proper window. It wasn't a frequent operation but it made me lazy about rebooting my computer to test something, and I was sure that Screen would allow to automate the process. In fact it could and it was very easy on top of that, so here's how I made it.

In the first place, I knew that I wanted to start the programs as if I had manually started them on each shell since I sometimes need to restart them (because of lost connections or external configuration changes). I needed to find out how to automatically make Screen:

  1. Create a new window.
  2. Give it a particular name.
  3. Run a shell in it.
  4. Inject the command for a program to be run in the shell.

Looking at the manual I found out that everything could be automated with Screen commands in a configuration file… hooray! The first three points with the screen command, which creates a new window with a title and the given command (or a shell if none is specified). The fourth one with the stuff command, which injects a string into the input of the program running on the current window. To jump right into the last window (a shell) one can use the at command to tell stuff to run in a window other than the current one.

So was born meinscreen, which is nothing more than a trivial script and a configuration file for Screen, ~/.meinscreenrc:

# Configuration for the main Screen session.

# System or user-wide configuration is included.
# source /etc/screenrc
source ~/.screenrc

# Some default windows are created.
screen -t mutt
screen -t gnus
# … more windows …
# Finally, a shell (active window).
screen

# Some tasks are started as they'd had been run manually.
at mutt stuff "mutt -f imaps://ivan@servidor.de.correu/INBOX\012"
at gnus stuff "emacs -nw -f gnus\012"
# … more programs …

The only contents of the meinscreen script are:

#!/bin/sh
exec screen -c ~/.meinscreenrc -S mein

Just run meinscreen on a terminal and… voilà! A Screen session with all programs running as started manually, each in its titled window, and straight into a shell. To attach this Screen session from an SSH session just run:

$ screen -dr mein

The original script name was myscreen, but since all Screen sessions are indeed mine and this is the main one, I renamed it to mainscreen. In the end meinscreen was chosen as a compromise. :D

Main screen turn on.