GNU screen is pretty badass

Posted in general news on October 26th, 2009 by tetujin

so i’ve been using screen for a couple years now, and i really dig it. i use it to keep tabs on my ubuntu box at home, where i run irssi on top of znc, apache, and a few other services. the only thing that bugged me up until now was that any time i needed to reboot (patches or power outages), i’d have to spend extra time interactively logging in and setting up my screen session all over again. i fixed all that this morning.  i did two things:

  • i did a little RTFM’ing of the screen man page so that i could write a custom .screenrc file (actually .screen.boot) that would start up exactly the way i wanted it to every time, and
  • i taught myself a little bit of bash so that i could write a boot script that would test for screen sessions and if none existed, would start up my custom screen for me so that it would be ready before i even logged in.  yeah, i’m a tcsh guy by nature.  i don’t know what to tell you.

first, i made a .screenrc.boot (as opposed to a normal .screenrc) to differentiate from a normal invocation of screen, because i only want this custom setup at boot time, and not every time i invoke screen.  don’t feel that you have to, but it might be a good idea.

following are some example commands to add to the end of your ~/.screenrc.boot.  check the man page for more details, or google for ‘screenrc’, there’s a lot of info out there.  thusly:

screen -t irssi 0 /home/alice/scripts/zncirssi
screen -t shell1 1 /bin/tcsh
screen -t shell2 2 /bin/tcsh
screen -t rtorrent 3 /usr/bin/rtorrent
screen -t alpine 4 /usr/bin/alpine

to explain the syntax briefly here:
screen -t title windownum process
this lets me give each window a name, and windownum is the window number i want each process to have, with the process to start at the end.

then, once i had my .screenrc.boot set up the way i liked, i created the boot script as /etc/init.d/screen-startup.   you can give it any name, really.  i cribbed my version from /etc/screen-cleanup.  don’t forget to link the finished script to /etc/rc3.d or wherever is most appropriate for your flavor of *nix, so that it actually executes at boot time.  here’s the script minus some of the irrelevant header comments:

#!/bin/sh
# Script to start screen on boot for users.
#

set -e

SCREENDIR=/var/run/screen

if [ "$(ls -A $SCREENDIR/S-alice | grep boot)" ]; then
    echo boot screen session already running for alice
else
    # -dm runs the screen detached,
    # -S lets me define a name for the session,
    # -c lets me pick my config file.
    sudo -u tonye /usr/bin/screen -dmS alice.boot -c /home/alice/.screenrc.boot
fi

exit 0

if you have multiple users who needs screens at boot time, feel free to add additional if conditionals.  i could probably set up a foreach, but i was lazy.

additionally, zncirssi is a short script that runs znc and then irssi right afterwards. if run alone, znc will fork into the background and then close the originating screen window. this way you can make sure znc forks and then once running, lets irssi run.

contents of zncirssi:

/usr/bin/znc ; /usr/bin/irssi

that’s pretty much it. i hope you find it useful.

edit: i found that the bash script didn’t launch screen the way i thought it should.  not sure why.

No comment so far