views:

2148

answers:

5

Is it possible to write a script to change the name and turn on monitoring for the current tab assuming that it is being run in screen?

Thanks.

A: 

Screen runs transparently, so detecting the screen session is hard. If you try sending key presses, that would work, and would fill your terminal with a tiny bit of junk if you're not in a session.

A. Scagnelli
Well, if I need to I can put something in my screenrc file like a variable declaration to make it obvious for my bash script (this script is just for me).I'm still hung up on the other part though. I have these programs that take several hours to run, and it'd be great if as soon as a start the program the title is renamed to the currently running process (my script is run with a single argument) and monitoring would turn on, so I could just hope to another tab and get notified when my program is done.
Dan
+1  A: 

Are you looking to display information or interact with the screen session itself? You can send messages back with this: (http://www.slac.stanford.edu/comp/unix/package/epics/extensions/iocConsole/screen.1.html#lbAI)

THE MESSAGE LINE Screen displays informational messages and other diagnostics in a message line. While this line is distributed to appear at the bottom of the screen, it can be defined to appear at the top of the screen during compilation. If your terminal has a status line defined in its termcap, screen will use this for displaying its messages, otherwise a line of the current screen will be temporarily overwritten and output will be momentarily interrupted. The message line is automatically removed after a few seconds delay, but it can also be removed early (on terminals without a status line) by beginning to type.

The message line facility can be used by an application running in the current window by means of the ANSI Privacy message control sequence. For instance, from within the shell, try something like:

echo '<esc>^Hello world from window '$WINDOW'<esc>\\'

where '' is an escape, '^' is a literal up-arrow, and '\' turns into a single backslash.

hometoast
+2  A: 

Screen declares $STY environment variable, you can try use it.

Piotr Findeisen
+3  A: 

I think this works:

if [-n "$STY"]; then
    screen -X title "foo"
    screen -X monitor on
fi
Joe Beda
Perfect! Thanks!
Dan
I don't think the "if [-n..." stuff is required - the screen -X ... command silently errors if it cannot run
dbr
With this technique I think I'll forget about monitoring, I'll just change the title to foo(done) when the program finishes. I still have to figure out when is a good time for it to change back to "bash" since I've never really used titles and don't really feel like changing them manually.
Dan
@Dan - you can use string escapes in your prompt to automagically change the title in screen. Bash is probably a little different, but here's how I do it in zsh: http://gist.github.com/143727
rampion
+2  A: 

From the screen manpage:

  -X   Send the specified command to a running screen  session.  You  can
       use  the  -d or -r option to tell screen to look only for attached
       or detached screen sessions. Note that this command  doesn't  work
       if the session is password protected.

Basically, run

screen -X title mynewtitle
screen -X monitor on

If the terminal is not running within screen, it silently errors:

notinscreen:~$ screen -X title mynewtitle
notinscreen:~$

You can also send the command to a specific session, even while detached. Given a screen session named "main":

$ screen -S main
[detach from screen session]
$ screen -ls
There is a screen on:
        2073.main       (Detached)
1 Socket in /tmp/uscreens/S-dbr.

..you can sent the title of the last-active window to blah:

$ screen -x main -X title blah

..or a specific window (named oldwindow in this example):

$ screen -x main -p oldwindow -X title blah

Instead of using -x main you can use the process ID (from screen -ls), also instead of using a window name (-p oldwindow) you can use a window number:

$ screen -x 2073 -p 0 -X title h

If there is only one session, you do not have to specify the session PID or name.

dbr