I am running an interactive curses program in a screen session on quite weak hardware. Most of the time, the screen is detached. Is there a way for this program to detect this situation, and basically stop painting on the screen?
There's an irssi script called
screen_away that sets your away status based on whether or not the screen is attached. It works like this:
Every shell running within a screen will have the
STY environment variable set to the socket name. You can run
screen -ls to get the path where all the sockets are stored:
$ screen -ls There are screens on: 11824.pts-24.mmrozek (Attached) 2306.ssh (Detached) 6615.chat (Attached) 3 Sockets in /var/run/screen/S-mmrozek.
Check the execute bit on
/var/run/screen/.../$STY. If it's set, the screen is attached; otherwise it's detached. You can see from the output above that two of my three screens are attached, and here the corresponding execute bits are set:
$ ls -l /var/run/screen/S-mmrozek/ total 0 prwx------ 1 mmrozek mmrozek 0 Oct 5 13:04 11824.pts-24.mmrozek prw------- 1 mmrozek mmrozek 0 Sep 23 23:45 2306.ssh prwx------ 1 mmrozek mmrozek 0 Oct 5 12:43 6615.chat
You could also just keep checking the output of
screen -ls for a row that contains
$STY, and see if it ends in
(Detached), but that requires running
screen over and over