Running commands on Putty without fear of losing connection

68,805

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

you can also set up your environment to log you in and start screen right off the bat. There are many ways to do it. I chose to add this to my .bashrc file.

# screen management
if [ $SSH_TTY ] && [ ! $WINDOW ]; then
  SCREENLIST=`screen -ls | grep 'Attached'`
  if [ $? -eq "0" ]; then
    echo -e "Screen is already running and attached:\n ${SCREENLIST}"
  else
    screen -U -R
  fi
fi

Solution 5

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>

Share:
68,805

Related videos on Youtube

Admin
Author by

Admin

Updated on September 17, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • Duncan Jones
    Duncan Jones almost 7 years
    Note: step 3 is entirely optional. If you close your PuTTY session without detaching, you can still reattach with screen -RD.