Recovering Unsaved Nano Editor Sessions After SSH Disconnection: Process Reattachment and Recovery Techniques


4 views

When your SSH session gets interrupted while working in nano, you might spot something interesting when running ps -ef | grep nano:

user@server:~$ ps -ef | grep nano
1001     31714 29481  0 18:32 pts/0    00:00:00 nano important_file.txt

This means your nano session is still running in the background, holding your unsaved changes hostage in terminal limbo.

The most straightforward solution is using reptyr (available in most Linux distros):

sudo apt-get install reptyr  # For Debian/Ubuntu
sudo yum install reptyr      # For CentOS/RHEL

# Find the process ID (from ps output)
reptyr 31714

If you encounter ptrace restrictions, temporarily allow them:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

When reptyr isn't an option, try these techniques:

# Method 1: Using screen's detach/reattach
screen -D -r [session_id]

# Method 2: Forcing save via signals (risky)
kill -USR1 31714  # Triggers save prompt in nano

Configure your environment to prevent future losses:

# ~/.nanorc configuration
set backup
set backupdir "~/.nano-backups/"
set locking

Or better yet, use tmux/screen for persistent sessions:

tmux new -s dev_session
# Later, after disconnect:
tmux attach -t dev_session

Nano creates swap files in the format .filename.txt.swp. To attempt recovery:

ls -a | grep .swp  # Find swap files
nano -r filename.txt  # Try recovering

For automated recovery scripts, consider this bash snippet:

#!/bin/bash
for swpfile in $(find ~ -name "*.swp" -type f); do
    original=${swpfile%.swp}
    echo "Found swap: $swpfile for $original"
    nano -r "$original"
done

When working remotely via SSH, it's common to encounter terminal disconnections that leave text editor sessions hanging. In this case, we can see the nano process (PID 31714) is still running but disconnected from any terminal:

ps -ef | grep nano
1001     31714 29481  0 18:32 pts/0    00:00:00 nano frugg_batch_processing

The most straightforward solution is to use reptyr to steal the process back to your new terminal:

sudo apt-get install reptyr  # For Debian/Ubuntu
reptyr 31714

If you encounter permission issues, you might need to:

echo 1 > /proc/sys/kernel/yama/ptrace_scope

If reattaching doesn't work, you can attempt to force-save the buffer:

# First find the temporary file:
ls -la /proc/31714/fd/

# Then copy the content:
cat /proc/31714/fd/3 > recovered_file.txt

Consider using screen or tmux for more resilient sessions:

# Using tmux example:
tmux new -s mysession
nano myfile.txt
# After disconnect:
tmux attach -t mysession

Nano sometimes creates recovery files in these locations:

ls ~/.local/share/nano/
ls /tmp/