21

I want to terminate an instance of GNU screen whilst preserving the process running inside it.

So I opened my regular terminal emulator program and executed screen. Then I used that instance of screen to execute ./script_x.sh. Now, for whatever reason, I want to quit using screen however script_x.sh hasn't yet completed its task and I'm not willing to forfeit my progress.

Can I keep the active process alive so as to continue working on it inside my regular terminal emulator?

chicks
  • 590
voices
  • 2,881

3 Answers3

31

TLDR: Practical answer: No.

Longer answer:

In theory you can. If you started something like nohup myprog & from inside the screen then it will continue running. It will ignore a hangup signal and will not have any input, but in theorie you could continue working with it.

In practise this is not the case. Thus unless you want to attach a debugger to the running process and rewrite its file handles and make sure that the process handles -1 signals when you close screen ... then the answer is no.

If you are ready to do this, I have a bookmark at home pointing to [SU] where someone did just that. Saved for awesomeness, not because it is easy and trivial to do.

Hennes
  • 65,804
  • 7
  • 115
  • 169
23

You can try using reptyr to reattach already running application to different terminal. It has some issues with sending process to background, though.

I just tested it (start htop inside screen, reptyr it to another terminal, kill screen), and it seemed to work alright. Still it's a really hacky solution, so no guarantees.

aland
  • 3,066
  • 18
  • 26
14

Yes (if "killing" is not really killing), that's actually exactly what screen is for. You should check the manual and try which one of these fits your needs:

C-a d
C-a C-d     (detach)      Detach screen from this terminal.

C-a D D     (pow_detach)  Detach and logout.

Then what you will need is the -D/-d -R/-r command line options of screen, depending on your choice.

user556625
  • 4,400