Running commands on Putty without fear of losing connection
Solution 1
With screen:
screen
- type
command
- Ctrl-A-D to detach from the screen
- logoff from the session
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
:
nohup command > logfile &
- Logoff from the session
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, usescreen -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>
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on September 17, 2022Comments
-
Admin almost 2 years
How do you use the "screen" command effectively?
Is it:Type "screen"
Type in command
Lose connection
Check back on lost session
How do I carry out step 4?
-
Duncan Jones almost 7 yearsNote: step 3 is entirely optional. If you close your PuTTY session without detaching, you can still reattach with
screen -RD
.