Pages

Screen, do your work in the background | linux

For computer programmers, working remotely with Putty through ssh connections is pretty common. Normally, what we do is that we connect to putty to a remote linux (or windows(cygwin)) server, hit some commands to do some instantaneous work. But the problem may arise when we hit a long running command. For example, you connect to oracle user and start using the sqlplus in oracle. You have a long running engine to run in oracle. Suppose it takes two hours, and after one hour of running of the procedure, the network connection is our for a few minutes. What happens is that you session will be destroyed and the process will be in undetermined state.

Now the solution.

What you could have done is that you could have run your process in the background, independent of the active ssh session. You could do that with VNC or other technologies but I am going to talk about screen here.

You should have screen installed on your server. There are different commands to install screen on different versions of linux.
What screen does is generate a background session where your actual work is done, and it is independent of the ssh sessions.
The concept of screen is to create a session, attach to a screen session, and detach from a screen session.

1) Start a screen session
screen -S <new_session_name>
2) See the list of Active Screen sessions
screen -ls or -list
3) To reattach to a detached screen session
screen -r <existing_session_name>
4) To detach an attached screen session
for another remote user
screen -d <session_name
detach from ones screen and get out of screen
CTRL + a + d
5) To attach a screen session that someone else is attached.
screen -dr <session_name>


6) How do i know if i am in a screen session
echo $STY 


If the above commands outputs any value, then you are in a screen session. If the output is empty, you are not in a screen session.


Or if you simply press the backspace button for a long time the screen starts flickering in white color, if you are in a screen session


7) How do I kill a screen session

screen -X -S [session # you want to kill] quit

Thats it.

No comments:

Post a Comment

If you like to say anything (good/bad), Please do not hesitate...