Useful: how to move a running process into a screen terminal


One of the useful things about using screen (a terminal multiplexer) console is that you are able to detach from the screen terminal while keeping your running session alive.  At a later time (such as when you arrive home from work), you can reattach and resume the same running session and view any output while you were gone.

Sometimes I’m in this situation: I need to leave but I just started a long running process that I don’t want to restart but I didn’t initially start it from within a screen terminal.  I want to avoid killing off the process and bring it over into a screen terminal so that it can continue to run uninterrupted.  Enter reptyr which will allow you to attach a process to another terminal console.

[The following steps were done on Ubuntu 12.04]

Install reptyr

sudo apt-get install reptyr
sudo vi /etc/sysctl.d/10-ptrace.conf

change this line:

kernel.yama.ptrace_scope = 0

Reboot for this sysctl value to take effect.

Now your system is ready to let you reattach running processes to a different teriminal.

Start a long running program, we’ll use ‘top’ for this example:

# top

Suspend the ‘top’ process

[1]+  Stopped                 top

List jobs

# jobs -l 
[1]+  2961 Stopped (signal)        top

Resume job 1 in the background

# bg %1 

Detach the process from terminal

# disown %1 
bash: warning: deleting stopped job 1 with process group 2961

Start screen then reattach the process:

# screen

Find the process of the ‘top’ process and reattach it to screen terminal

# pgrep top 

# reptyr 2961

You are now running the ‘top’ process inside the screen terminal.  You may proceed to detach from screen and go home and reattach to it at a later time.

This entry was posted in Linux. Bookmark the permalink.