Richard Bullington-McGuire ([info]obscurerichard) wrote,
@ 2007-09-06 10:33:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Current mood:geeky
Entry tags:birthday, geek, harry potter, programming

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




(24 comments) - (Post a new comment)


[info]steeltoe
2007-09-06 03:33 pm UTC (link)
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.

(Reply to this) (Thread)


[info]steeltoe
2007-09-06 03:40 pm UTC (link)
also, happy birthday, dork.

(Reply to this) (Parent)(Thread)


[info]obscurerichard
2007-09-07 04:24 pm UTC (link)
Thanks.

(Reply to this) (Parent)


[info]obscurerichard
2007-09-07 04:22 pm UTC (link)
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.

(Reply to this) (Parent)(Thread)


[info]steeltoe
2007-09-07 05:44 pm UTC (link)
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.

(Reply to this) (Parent)


[info]blindtillnow
2007-09-06 03:38 pm UTC (link)
happy birthday! dork.

:D

-S

(Reply to this) (Thread)


[info]obscurerichard
2007-09-07 04:25 pm UTC (link)
What is this, gang up on the dork day with the backhanded happy birthdays?

Thank you, you big lit-snob.

(Reply to this) (Parent)(Thread)


[info]blindtillnow
2007-09-07 05:01 pm UTC (link)
nah, just like making fun of like. (though my dork tends to show more in the RP, LARP and collectible card games.... ;)

-S

(Reply to this) (Parent)


[info]prakriti
2007-09-06 05:25 pm UTC (link)
happy birthday!

All our mail is done via IMAP, which solves the multiple pine sessions issue quite nicely.

(Reply to this) (Thread)


[info]obscurerichard
2007-09-07 04:26 pm UTC (link)
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.

(Reply to this) (Parent)


[info]nalroth
2007-09-06 05:25 pm UTC (link)
HAPPY BIRTHDAY!!!!

(Reply to this) (Thread)


[info]obscurerichard
2007-09-07 04:26 pm UTC (link)
Thanks!

(Reply to this) (Parent)


[info]shadow27
2007-09-06 05:38 pm UTC (link)
That looks brilliant, but as a lay user it's all greek to me.

Nonetheless Happy Birthday and many returns.

(Reply to this) (Thread)


[info]obscurerichard
2007-09-07 04:26 pm UTC (link)
Thank you for your kind words.

(Reply to this) (Parent)


[info]zwieblumen
2007-09-06 05:48 pm UTC (link)
Happy birthday, and nice fan-fic :)

(Reply to this) (Thread)


[info]obscurerichard
2007-09-07 04:27 pm UTC (link)
Thank you!

(Reply to this) (Parent)


[info]eac
2007-09-06 05:55 pm UTC (link)
Happy Birthday!

(Also, this is cool. :)

(Reply to this) (Thread)


[info]obscurerichard
2007-09-07 04:27 pm UTC (link)
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.

(Reply to this) (Parent)


[info]alicelee
2007-09-06 06:42 pm UTC (link)
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.

(Reply to this) (Thread)


[info]obscurerichard
2007-09-07 04:24 pm UTC (link)
You are dead on with your "hobbit tradition" thing, though I wasn't thinking consciously of hobbits at the time. Thank you.

(Reply to this) (Parent)

I can't help appreciate...
[info]grassyknoll67
2007-09-06 08:13 pm UTC (link)
the extra-special geekiness of this approach

(Reply to this) (Thread)

Re: I can't help appreciate...
[info]obscurerichard
2007-09-07 04:27 pm UTC (link)
It is geek-licious.

(Reply to this) (Parent)(Thread)

Re: I can't help appreciate...
[info]grassyknoll67
2007-09-07 04:34 pm UTC (link)
that's just the way I like my women...

(Reply to this) (Parent)


[info]lalartu
2007-09-08 04:41 pm UTC (link)
Happy birthday.

I tend to use CTRL-X for local screens and CTRL-A for remote (I nest them)

(Reply to this)


(24 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…