Skip to main content

Screen

I'm a heavy user of the program screen, particularly at work where I connect to some database servers and automatically start screen as part of my .bash_profile or .bashrc files. (My .bashrc file is actually pretty complicated as I share my startup script between a variety of linux containers and systems at home and work so they call other bash scripts depending on the environment and host type.)

I'm also aware that a lot of people view tmux as a better alternative, but I've discovered it's not on all the systems and Operating Systems that I have a tendency to jump around on (although that is slowly changing). At some point in the near future, I might end up switching, but it will take me personally some time to learn how to use it.

As one screen file for conneting to a postgres database server, my .screenrc file looks like:

    term xterm

    hardstatus alwayslastline
    hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%? (%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'

    startup_message off
    screen -T xterm -ln -t top       top -c
    screen -T xterm -ln -t messages  sudo tail -f /var/log/messages
    #screen -T xterm -ln -t pgtop     pgtop
    screen -T xterm -ln -t pglog     sudo tail -f /var/log/postgres

    logfile $HOME/logs/screen/screen_%H_%Y-%m-%d_%0c-%n

If you need help understanding my .screenrc file, please see the documentation (at https://www.gnu.org/software/screen/)

But I've also had the problem where I couldn't re-connect to an existing screen session (usually because I've hosed my terminal session, or I've needed to connect to another user's screen session because I've started a screen session under a different username on one of my test systems.).

I received the following message:

Cannot open your terminal '/dev/pts/1' - please check.

I found an answer to this by executing the following code:

    $ sudo su _user_
    $ script /dev/null
    $ screen -r _screen session id_

This allowed me to connect and then terminate that user's session. (And yes, I should be running more things under my userid rather than another user's, but sometimes I have to and that's the beauty of a test environment!)