Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
i am just curious: what are some of the reasons you would run a detached screen? – amphibient Oct 5 '12 at 17:07
@foampile If you're never going to detach the screen, what's the point of running within screen at all? – Michael Mrozek Oct 5 '12 at 17:08
up vote 8 down vote accepted

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 (Attached) or (Detached), but that requires running screen over and over

share|improve this answer
Ha, learned something new today. Any idea if that execute bit means anything more than just a "attached/detached" flag? – jw013 Oct 5 '12 at 18:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.