Puzzled by screen’s stuff

One little problem I’ve been trying to decipher for my own benefit is a script to shut down screen. I usually have five or six applications running in screen, and when I close the machine for shutdown, I manually step through each program, giving it the command to close (and sometimes save).

Since it’s possible to pipe keystrokes through screen to the appropriate application, it seems to follow that a simple five- or six-line series of commands could cleanly save and close everything I usually have running. Technically, the command looks like this:

screen -p elinks -X stuff Q

A shifted Q is a quick shutdown command for elinks, and sending that through screen to the window entitled “elinks” (which is always called “elinks,” because it’s triggered at startup in my .screenrc) causes the application to stop. This will shut down alpine, since the command there is a q, followed by a y to answer yes to the confirmation.

screen -p alpine -X stuff qy

And so forth and so on. htop can close with a q, wyrd with a shifted Q. The problem comes with applications that require function keys as shutdown commands, such as mc‘s F10 close command, or a series of Escape keys for centerim, or hnb which wants a CTRL+Q to quit.

For some reason, using the keystroke sequences in the GNU/Screen user’s manual don’t enter as escaped, instead they spill onto the screen as typed characters, or are ignored since they don’t form an escape sequence. So if I try this, which I think sends an F10 command to Midnight Commander:

screen -p mc -X stuff \033[21~

The console command line in mc shows 033[21~ after the command prompt, as if I had typed those letters directly myself. Chances are I’m feeding it the wrong commands, or there is some other special switch that I need to add to get it to actually send through as a keystroke instead of raw text.

I could probably just use screen’s default kill switch (CTRL+A CTRL+\) to close everything, but I don’t know if I am comfortable with that. Given that these commands are available and work for some cases, I probably just need a nudge to get the whole thing working as imagined. 😐


9 thoughts on “Puzzled by screen’s stuff

  1. William

    So a little research into your programs and see which ones can be shut down safely via SIGTERM. The ones that can, you would use the kill command to send SIGTERM to all of those processes.

    1. K.Mandla Post author

      I probably could. The added bonus in getting this to work (as I imagine it) would be that I could tie the script to a preprogrammed keystroke. That would give me a one-key command to automatically save open notepads, close all the applications neatly, exit screen and be ready for a shutdown.

      Or that’s how it would work in my little world. … πŸ™„

      1. vespas

        if apps shutdown safely with SIGTERM then you could just shut down the machine, as init sends a SIGTERM to everything! I’m guessing that other than unsaved buffers in vi (or similar), it doesn’t really matter if you just kill everything.

  2. foo

    I prefer using the termcap name instead of the command sequence.
    screen -p mc -X stuff -k “k;”
    should do the trick for you.

    You may also want to have a look at tmux, it’s easier to configure and under active development.

  3. saintsteele

    To shut down rtorrent (which I think requires ctrl-q), I use “screen -X -S tor eval ‘stuff 21′” (without the double quotes, fixing wordpress’s “smart” quotes, and “tor” is always the name of my rtorrent session). Probably “-p” is a better choice than “-S”, although it’s been working fine for me.

    1. saintsteele

      Wow, I just realized that wordpress awesomely stripped out part of the command I just posted: instead of “21”, that should have said the equivalent of backslash-zero-two-one.

      1. boldewyn

        It seems, that eval and the -p switch don’t play well together. At least on my machine, the string is always stuffed in the current window.

  4. Boldewyn

    I found this post through Google, because I had the same problem. However, using xterm and bash under Win/Cygwin I solved it by typing the control characters via the Ctrl-V escaping:

    screen -X -p mc stuff [Ctrl-V][F10][Ctrl-V][Enter]

    which just did as requested.

  5. Pingback: Progress on the screen shutdown script « Motho ke motho ka botho

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