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?

  • i am just curious: what are some of the reasons you would run a detached screen?
    – amphibient
    Commented Oct 5, 2012 at 17:07
  • 3
    @foampile If you're never going to detach the screen, what's the point of running within screen at all? Commented Oct 5, 2012 at 17:08

1 Answer 1


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

  • 1
    Ha, learned something new today. Any idea if that execute bit means anything more than just a "attached/detached" flag?
    – jw013
    Commented Oct 5, 2012 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .