Running commands on Putty without fear of losing connection

How do you use the "screen" command effectively?
Is it:

  1. Type "screen"

  2. Type in command

  3. Lose connection

  4. Check back on lost session

How do I carry out step 4?


With screen:

  1. screen
  2. type command
  3. Ctrl-A-D to detach from the screen
  4. logoff from the session
  5. screen -RD to reattach to the screen (if there's more than one you'll get a list of current screen sessions and you'll have to supply the session number) from a new session

A simpler method that is useful for commands that leave logfiles or just throw some relevant output and do not need interactivity is nohup:

  1. nohup command > logfile &
  2. Logoff from the session
  3. tail -f logfile from a new session

Here's what I have picked up about using screen (which I, too, have just started doing):

  • screen -S <name> creates a screen named '<name>'. This is quite useful if you want to have several screen sessions going at the same time. For instance, I have one I use normally and one I use for my persistent processes.
  • screen -ls lists the running screens.
  • screen -r <name> resumes a detached screen. If the screen is already attached somewhere, use screen -dr <name>.

Also, when you start using screen, whenever you plan to leave, press ^a d (= ctrl-a followed by a d) to detach the screen you're currently running. It can then be resumed later.

In addition, I can recommend taking a look at ^a ? for a list of the different commands you can use while inside of a screen.

The most important of these (to me) are:

  • ^a c to create a new window in your screen session.
  • ^a ^a to switch between the two last used windows.
  • ^a " to list the current windows in your session.
  • ^a Esc to scroll in your screen buffer.
  • ^a k to kill the current window.
  • ^a x to lock your screen session, in case you need to leave your computer and don't want people to mess with it.

Screen is really powerful, and allows you to do exactly what you asked.

To see all your sessions, type

screen -list

Once you've identified a screen session to reconnect too, try

screen -dr SCREENID

which will nicely detach and re-attach your session.

You can also do a less nice,

screen -D -R

which will detach and logout remotely, if nesscessary, then reattach, or if that session doesn't exist, it will create it and notify the user. You can add a "-t NAME" to give shells or programs a title.

Within screen, use

command-c

to create a new window (So you don't need 4000000 screen sessions to disconnect and reconnect from), and change between them with

command-int

Indexing from 0, of course:P

Since you seem a bit unfamiliar with screen, I'm going to assume you could use some other info. I like the following commands, like:

-e xy

Which causes x to be the command character and y to be the character to generate a literal command character. The defaults are Ctrl-a and `a.

There's some more for you here: Screen! It's what's for programmers


screen -dr to detach and resume the previous screen session.


A quick Google search found this screen guide

http://www.rackaid.com/resources/linux-tutorials/general-tutorials/using-screen/

So from step b) you can detach the screen using :

"Ctrl - A" "d"

and then later after the connection has been lost and your connected once again find the screen using :

# screen -ls

and then reattach using :

# screen -r <screen_session_name>