0

From my home pc using putty, I ssh'ed into a remote server, and I ran a python program that takes hours to complete, and as it runs it prints stuff. Now after a while, my internet disconnected, and I had to close and re-open putty and ssh back in. If I type 'top' I can see the python program running in the background with its PID number. Is there a command I can use to basically re-open that process and see it printing its stuff again?

Thanks

omega
  • 5,797

2 Answers2

2

gnu screen http://www.gnu.org/software/screen/ is an application that runs in a terminal, creating a sort of virtual session. When you disconnect, screen continues to run as a detached entity, to which you can then re-connect later.

First, install screen if it is not already installed (it probably is).

Then log in to your server.

Start a screen session:

$ screen

Now you are in screen. Start your process. I'll assume it's called foo.py. You can start it as normal, or maybe you prefer to start it and send it to the background:

$ foo.py &

If you would rather NOT send it to the background (that is, you want to see it working in your terminal), then just start it normally - but then you will want to switch to a new screen:

$ foo.py

One the script starts running, press ctrl-a ctrl-c (that is, Create new screen).

This creates a new empty screen. Your script is still running, but it's running on the other screen. Prove it to yourself by typing ctrl-a ctrl-n (that is, go to Next screen).

When you are ready to log out, type:

$ screen -d

That detaches you from the virtual screen. In other words, everything is STILL running, just as you left it.

You can even close puTTY. Your server is still running screen and whatever you launched inside of it.

When you want to re-connect, log back on and re-attach to screen:

$ screen -raAd

Now you're back in screen. You might see the output of your foo.py command, or you may need to switch over to it with ctrl-a ctrl-n.

Either way, that's how it's done.

2

You can use screen command and start a job such as compiling kernel such as :

screen -R -S YOUR_SESSION_NAME

Then press entern to return to session

for exit to session : CTRL+a+d

For listing your sessions:

mohsen@debian:~$ screen -ls
There is a screen on:
    30473.compilekernel (03/06/2015 05:59:05 AM)    (Detached)
1 Socket in /var/run/screen/S-mohsen.

For detatch to your session:

screen -dr 30473

When you don't use session name screen itself uses the following format and it's very hard to multiple session for your server:

pid.tty.host

such as :

30522.pts-0.debian
PersianGulf
  • 10,850