How to Move an Already Running Process to GNU Screen for Persistent Remote Execution


4 views

When working remotely via VNC on Linux systems, we often encounter situations where a process initiated in the terminal outlives our work session. A common scenario:

$ ./data_processor.sh large_dataset.csv
# After 3 hours...
^C # Oops, this will take 12 more hours!

GNU Screen allows terminal session persistence, but the challenge is migrating existing processes. This differs from simply starting new processes in Screen.

Prerequisites: GNU Screen installed on the remote Linux box (sudo apt install screen for Debian/Ubuntu)

Method 1: Using reptyr (recommended)

# On the remote server:
$ sudo apt install reptyr
$ screen -S persistent_work
$ ps aux | grep data_processor.sh # Find PID
$ reptyr 12345 # Replace with your process ID

Method 2: Using gdb (advanced)

$ screen -S migration_target
$ gdb -p 12345
(gdb) call fork()
(gdb) call dup2(open("/dev/pts/X", 1), 1) # Replace X with actual pts
(gdb) call dup2(open("/dev/pts/X", 1), 2)
(gdb) detach
(gdb) quit

Imagine you started a PostgreSQL dump without Screen:

$ pg_dump large_db > backup.sql
# Realize it's taking hours...

Migration steps:

$ ps aux | grep pg_dump # Find PID 67890
$ screen -S db_backup
$ reptyr 67890
# Ctrl+A then D to detach
  • Processes must be single-threaded for reptyr
  • Root access may be required (echo 0 > /proc/sys/kernel/yama/ptrace_scope)
  • Some interactive programs may behave differently after migration

For complex process trees, consider using screenify:

$ git clone https://github.com/npryce/screenify.git
$ ./screenify 12345

This handles entire process groups more gracefully than reptyr alone.

For regular use, add this to your .bashrc:

alias attach='screen -dr || screen -S main'

Now you can simply ssh in and run attach to resume work.


When working remotely via VNC on a Linux server, we often encounter situations where a process takes much longer to complete than initially expected. This becomes problematic when you need to disconnect your workstation while keeping the process running.

The screen utility allows processes to continue running even after you disconnect. While it's ideal to start processes in screen initially, we often need to migrate already-running processes.

Here's how to move an existing process into a screen session:

# First, identify the process ID (PID) of your running job
ps aux | grep [process_name]

# Attach the debugger to the process
gdb -p [PID]

# In the GDB prompt, detach the process from its current terminal
(gdb) call dup2(open("/dev/null", 0), 0)
(gdb) call dup2(open("/dev/null", 0), 1)
(gdb) call dup2(open("/dev/null", 0), 2)
(gdb) detach
(gdb) quit

# Now create a new screen session
screen -S migrated_process

# Reattach the process to screen
reptyr [PID]

For a simpler approach (requires root or appropriate permissions):

# Install reptyr if needed
sudo apt-get install reptyr  # Debian/Ubuntu
sudo yum install reptyr      # CentOS/RHEL

# Then simply:
reptyr [PID]
  • Some processes might not survive the migration (particularly those with complex terminal interactions)
  • For critical processes, consider testing with non-essential jobs first
  • The process must not be a session leader (daemons typically won't work)

To ensure the process stays running after you disconnect from screen:

# Inside your screen session:
screen -S long_running_job
# Start or attach your process here
# Then detach with Ctrl+A, D

If you encounter "Permission denied" with reptyr:

# Temporary solution (security implications):
echo 0 > /proc/sys/kernel/yama/ptrace_scope

# Permanent solution (edit sysctl.conf):
sudo sysctl kernel.yama.ptrace_scope=0