Donate

Categories

Advert

XHTML

Valid XHTML 1.0 Transitional

Maintaining Screen Output

In my post about getting started with KVM I noted the fact that I had problems keeping screen output after the program exits [1].

The following snippet of shell code demonstrates the solution I’ve discovered for this problem. It determines whether SCREEN is the parent process of the shell script and if so it sleeps for 60 seconds before exiting so I can see the KVM error messages. The other option is for the script to call “exec bash” to give me a new shell in the same window. Note that if I start a screen session and then run my KVM script I don’t want it to do anything special on exit as I will return to the command-line in the same window. If I run “exec kvm-unstable” or have a system boot script run “start-stop-daemon -S -c USER --exec /usr/bin/screen -- -S kvm-unstable -d -m /usr/local/bin/kvm-unstable” then on exit I will be able to see what happened.

#!/bin/bash
set -e
kvm ETC || echo “KVM gave an error return code”
COUNT=$(ps aux|grep $PPID|grep SCREEN|wc -l)
if [ "$COUNT" = "1" ]; then
  echo "screen is the parent"
  sleep 60
else
  echo no screen
fi

Update: Thanks to John for the Slee for suggesting the following:
#!/bin/bash
set -e
kvm ETC || echo “KVM gave an error return code”
if grep -q SCREEN /proc/$PPID/cmdline ; then
  echo "screen is the parent"
  sleep 60
else
  echo no screen
fi

8 comments to Maintaining Screen Output

  • if grep -q SCREEN /proc/$PPID/cmdline ; then

    fi

    Thanks for writing this up. Is interesting

  • Iustin Pop

    Are you aware of the Ganeti project (disclaimer: one of the authors, etc., etc.) which can be used to also manage KVM machines on a single machine, allowing you to stop caring about such particulars as how to start KVM, how to manage storage, etc.?

    See https://penta.debconf.org/dc9_schedule/track/DebConf/363.en.html for a talk at DebConf 2009 about this way of using Ganeti.

    HTH,
    iustin

  • Martijn Grendelman

    Hi,

    There are two more options to keep screen output:
    – You can tell sceen to log all output to a file, with the -L option
    – If you put

    zombie “3315”

    in your .screenrc, screen will not terminate upon exit of the shell script, but instead it will print a line, that looks something like:

    === Command terminated with signal 15 (Thu Mar 18 12:29:57 2010) ===

    and keep the screen alive, so you can attach to it just like when the program was still running. If you then press ESC (33), screen will exit, or if you hit enter (15) screen will try to restart the program it was running before.

    Useful?

    Regards,
    Martijn.

  • Martijn Grendelman

    Hm, your blog mangled my comment a little bit. The keys that are arguments to the ‘zombie’ command were actually octal representations, preceded by a backslash and a zero. Let me try to work around this:

    zombie “\033\015″

    And what’s wrong with the time here? The comment before mine was posted at 12:49 pm, and mine at 9:33 pm :-)

    Regards,
    Martijn.

  • jshare

    Martijn,
    I just use “zombie kr” (for “k”ill or “r”estart). :)

  • etbe

    Iustin Pop: Sounds interesting, is it possible to download videos of those talks?

    Martijn: Thanks for that suggestion, I’ve edited your comment to have the correct data. I’m not sure why it didn’t work when you typed it in, once again it’s “\033\015″. As for the time, I’m running my blog in Eastern Australia non-daylight savings time – that is +1000.

    jshare: Thanks for that. Not only is your suggestion easier to write in a blog comment but it’s more convenient to type when running screen (IMHO).

  • Iustin Pop

    etbe: There are, as for any DebConf talks… Check the links on this page: https://penta.debconf.org/dc9_schedule/events/363.en.html

  • etbe

    Iustin Pop: Thanks for that. The talk was interesting and Ganeti sounds like a very useful product and I will keep it in mind for future projects. I don’t think it solves my needs though, all I want is a basic way of maintaining the console and screen seems to do the job well – particularly now that I’ve been advised how to use the “zombie” command.

    I may use libvirt/virt-manager as suggested by Aigars if I find screen to be inadequate. I really want something relatively light-weight and Ganati seems to be bigger than I desire.