Harry Potter shell script fan fiction, in celebration of my 35th birthday

When I have started multiple terminal sessions to server computers via SSH, I often run into problems when I forget that I have started a program in a different login session. Running pine multiple times is the worst, because of the little locking dance it does with the other pine sessions. Just thinking about it makes me unbearably sad, as if all the joy had drained out of the world. So, to defend myself against these ghostly login sessions, I now invoke the Patronus Charm:

[rbulling@tiamat:~/public_html/linux]$ patronus
Lumos!

Traversing floo network at pts/2, dementors spotted at:
pts/11
pts/5

Specialis Revelio!

Dementors found:
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
rbulling 18987  0.0  0.1  7000 2164 ?        S    10:45   0:00 sshd: rbulling@pts/5
rbulling 18993  0.2  0.0  5328 1372 pts/5    S    10:45   0:00 -bash
rbulling 19051  0.1  0.1  8048 2432 pts/5    S    10:45   0:00 elinks http://www.jkrowling.com/textonly/en/
rbulling 19105  0.0  0.1  7000 2148 ?        S    10:45   0:00 sshd: rbulling@pts/11
rbulling 19110  0.2  0.0  5332 1372 pts/11   S    10:45   0:00 -bash
rbulling 19173  3.2  0.1 12528 3472 pts/11   S    10:45   0:00 pine -i

EXPECTO PATRONUM!

[rbulling@tiamat:~/public_html/linux]$

After running this, all the processes associated with the other terminal sessions die, so you are left with one and only one working login session. Unlike issuing kill -HUP -1, the traditional way to deal with this problem, the patronus script will not harm processes associated with your current terminal session, or any daemon processes you may own. So, you can even run it safely as root.

To use this, you may want to use this plain text-version. I’ll keep that location updated with any changes.

#!/bin/sh
#
# patronus
#
# Dispel ghost logins that suck the joy out of your active terminal session
#
# Copyright (C) 2007 Richard Bullington-McGuire
# Many thanks (and aplologies) to J.K. Rowling
#
# DISCLAIMER
#   This is a parody of J.K. Rowling's Harry Potter novels.
#   Lawyers: please don't sue this poor house-elf who is only trying to help.
#
# LICENSE
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Release History:
#
# 1.0 (September 6, 2007)
#  First public release

retval=1
MAGICAL_CREATURES=`mktemp -t patronus.XXXXXXXXXX` && {
        FLOO_NETWORK=`mktemp -t patronus.XXXXXXXXXX` && {

                echo "Lumos!"
                echo ""

                ps x > $MAGICAL_CREATURES

                FLOO_EXITS=`egrep " *$$" $MAGICAL_CREATURES | 
                         awk '{print $2}'`
                awk '!/ PID/{print $2}' < $MAGICAL_CREATURES | 
                        sort -u |
                        egrep -v "^?|$FLOO_EXITS" > $FLOO_NETWORK
                DEMENTORS=`cat $FLOO_NETWORK`
                if [ -z "$DEMENTORS" ] ; then
                        echo "No dementors spotted nearby."
                        retval=0
                else
                        DEMENTOR_KISSES=`grep -f $FLOO_NETWORK < $MAGICAL_CREATURES | 
                                awk '{print $1}'`
                        DEMENTOR_DESCRIPTION=`ps u $DEMENTOR_KISSES`

                        cat << EOT
Traversing floo network at $FLOO_EXITS, dementors spotted at:
$DEMENTORS

Specialis Revelio!

Dementors found:
$DEMENTOR_DESCRIPTION

EXPECTO PATRONUM!

EOT
                        kill -HUP $DEMENTOR_KISSES 2>/dev/null
                        retval=$?
                fi
                rm -f $FLOO_NETWORK
        }
        rm -f $MAGICAL_CREATURES
}
exit $retval

(mad props to Oleg Parashchenko’s fabulous http://tohtml.com/ for color syntax highlighting)

I wonder if this is the first published example of Harry Potter fan fiction in Bourne shell script

Advertisements

24 thoughts on “Harry Potter shell script fan fiction, in celebration of my 35th birthday”

  1. I solved this (and some other concurrency problems) with changing the way I work by using screen.

    Now ALL of my terminals sstart and share the same screen session. I can continue from anywhere any program from any of my terminals.

    Having the same screen session in multiple terminals is *wonderful* and really really helps my productivity.

    The magic is surprsingly easy – this is the last line of my .bash_profile:

    (screen -x bsherman || screen -S bsherman) && exit

    It attaches to a screen if one exists, creates one if it doesn’t, and exits when I detach.

    I also tweaked my screenrc a bit to give me a useful status line:


    caption always "%{= bc} [%-w%{= bW}%n %t%{= bc}%+w]%=[%H][%c %D %M %d] "
    # %{= bc} blue, cyan for default text
    # %-w all windows up to current window
    # then we display the current window
    # %{= BW} Bright Blue Background, White foreground
    # %n screen number
    # %t screen title
    # %{= bc } go back to regular formatting
    # %+w all windows after the current one
    # %= pad with spaces
    # %H hostname
    # %c %D %M %d 24 hour time, day, month, day of month

    Then you can do some fun stuff with your PS1 var to set the title of the screen window.

    I *love* the way that this works, and if you can get used to being in a screen session all of the time (it doesn’t take long), it’s super easy to work with.

  2. This makes me all happy. Perhaps you are following the hobbit tradition of giving gifts to others on your birthday. Welcome to the other side of 35.

  3. That is a neat trick. I think I’ll experiment with this. The only gotcha is the C-a escape character jamming me up when I try to go to BOL in bash, because I use emacs command line editing there. So, I would have to get used to “C-a a” instead. The status line hack is very, very nice.

    I used to use screen more about 12 years ago, but I fell out of the habit. BOFH sysadimins tend to frown on screen since some people use it in resource-pig ways.

  4. You are dead on with your “hobbit tradition” thing, though I wasn’t thinking consciously of hobbits at the time. Thank you.

  5. That would solve the problem nicely. Obscure will move that way eventually, to an all-IMAP setup, but not today.

    Thank you for your birthday wishes.

  6. Thank you!

    I have had this under my belt for a few weeks, I just had to make it a little funnier in the source code itself before I published.

  7. nah, just like making fun of like. (though my dork tends to show more in the RP, LARP and collectible card games…. ๐Ÿ˜‰

    -S

  8. Lots of people just switch C-a to something else. I haven’t found a suitable substitute, plus my fingers are already used to C-a a.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s