Keeping a process alive after closing putty session
I found recently while working in my new office that if you have an ADSL connection and use putty the SSH client you regularly get the dreaded “Connection Closed“ error popping up and you lose all your work that you have done so far. This happened to me so often that I decided to try and find a solution, thankfully there is a simple way to fix this and also make you more productive while coding. Win:Win situation.
It’s so simple you will wonder how you lived without it.
What I am talking about is a program called Screen for Linux. Screen will keep what ever your working on open even when your connection drops out. It’s very simple to use and has some great features.
So lets see if we have screen installed on our server. The easiest way to do this is to just type : “which screen“
This will then tell you where and if screen is installed on your server.
If you get something like this then screen is installed, if you just get a black result then its not installed and you will have to install it. Try “apt-get install screen“
after you type “screen” your command prompt will usually just go to the next line, it looks like nothing has happened but thats screen started.
ok so we can now go ahead and do our work as usual for example you are downloading a file from the internet.
we then decide that we want to start coding up a new css file for your favourite site. All we have to do is open up a new screen window. To do this all we do is press “Ctrl + A” then “c” this will open up a new window that we can start coding in.
To swap between the screen all we have to do is type : “Ctrl + A” then “n“. you can open as many windows as you need and swap between them in various ways using the ”Ctrl + A” shortcut. You can see all the different commands by typing “man screen“.
So then out of the blue it happens again, the connection drops, normally you would have lost all your work and would have to log in to your server again and delete the swap file and start your download all over again but not when your running screen.
just log back in to your server then type : ”screen - ls” to list any open screen windows.
Then to re-attach that screen just type : “screen -r PID” so for this example it would be ”screen -r 28772” you will then be back in to the selected screen and it will be just as you left it so your download will still be downloading and your vim session will still be active. Just remember that you need to re-attach all the screens before you can swap between them again.
To close the screen your working on but leave it running in the background type: ”Ctr + A” and “d” and you will revert to one of your other screen windows or the shell if you only have one.
To kill the session you can either type “exit” or type ”Ctr + A” and “k” on the screen window you have open.
I found this massively helpful and increased my productivity greatly. Give it a try and hopefully you will love it as much as I do.




Posted under: 

