| Richard Bullington-McGuire ( @ 2007-09-06 10:33:00 |
| 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…