Wednesday, April 24, 2013

Running programs on server using Screens command

In my last blog I have discussed a way of connecting to your server PC at home from anywhere over the internet either on another desktop or on mobile device. I faced a problem of getting disconnected from the server when the phone signals were week on the move. So tasks like compiling of android source code was halted as soon as I get disconnected from the ssh client. Here is a way to overcome this problem which i found on the internet:

Login to the server using the ssh client and start a screen on the server. Now the shell will actually be running on the server rather than the client.
$ screen
Any other ssh client can now get connected to this screen and can view a copy the screen on their local shell. Even if the client gets disconnected due to any reason, the shell will still be running on the server.

To display the screens running on server:

jaiprakash@jaiprakash-Inspiron-1525:~$ screen -ls

There are screens on:

    2941.pts-4.jaiprakash-Inspiron-1525    (Wednesday 24 April 2013 11:15:52  IST)    (Attached)

    2811.pts-0.jaiprakash-Inspiron-1525    (Wednesday 24 April 2013 11:14:33  IST)    (Attached)

2 Sockets in /var/run/screen/S-jaiprakash.

To connect to a particular screen:
$ screen -x 2941.pts-4.jaiprakash-Inspiron-1525        --> to get a copy of the screen i.e. the shell running on the server.

if only one screen is running the simply use the command
$ screen -x

To disconnect from the screen (but process should be running on the server, in other words only to disconnect the client) use ctrl+A and ctrl+D or ctrl+A and type :detach
You would now be disconnected from the screen. but the process would still be running on the server.

I found it very useful for myself while using the ssh over internet. The screen command is very useful when the client gets disconnected abruptly. Hope this helps to remote developers :-P.

Reference: http://www.howtogeek.com/howto/ubuntu/keep-your-ssh-session-running-when-you-disconnect/

No comments:

Post a Comment