SSH

Using screen – Linux

This screen command in linux is best explained here. However, what I really wanted to do is to have a command running even after disconnecting from a remote server via SSH. In its simplest sense, it allows you to create a virtual terminal that can be attached and detached anytime you want.

Strictly Terminal Only (No X)

We are not talking about commands run in Gnome or KDE or XFCE here. We are talking about terminal only or SSH session where X is not available. Okay, let’s repeat it once again, screen allows you to create a virtual terminal/window that will act as if it were a new session. Whatever you do to that new terminal will only occur on that virtual terminal. When you exit to the virtual terminal, you will go back to the original terminal session.

To create a new screen window, type screen in your console.

screen

After executing that command, your terminal may react weird, then you are now in a new virtual terminal as if it were a fresh login. You can do the same task as if you were in the original session. To return to the original session, simply type exit.

Attach / Detach

You can also leave the new screen without terminating its session by detaching it. First you must be inside a screen – you need to invoke screen to create one. To detach from a screen, press the following:

CTRL + a then press "d"

# In the docs, CTRL + a is written as C-a, so it looks like

C-a d

You press CTRL + a then you press letter d. This will detach you from that screen window and return to the original session terminal. However, that detached screen is still running and will continue to run even if you logout your current session.

To connect back the detached screen – (to attach), you need to know the screen session ids. To list the running screen sessions, type:

screen -list

This will print all of your screen sessions with details such as the screen id and the status whether it is attached or detached. If you get lost and don’t know whether you are inside a screen session or not, invoke this command. It you see a single attached screen, then you are inside that screen.

The screen session id is in the format of pid.tty.host. Now that you know the scree session id, we can now attach it back and continue what you had left on that session.

screen -r pid.tty.host

Typical Usage

Some of my friends on IRC are always online 24/7. It’s because they use screen + IRSSI as their IRS client. What they do is put the IRSSI session on a screen and when they want to quit, they simply detach the screen and logout from their server. Of course their server needs to be online 24/7.

In my case, I do use screen to create a poor man’s cron. I’ll be posting about it on the next post.

1 thought on “Using screen – Linux”

Leave a reply

Your email address will not be published. Required fields are marked *