Skip to content

Instantly share code, notes, and snippets.

@OhadRubin
Created January 4, 2026 11:08
Show Gist options
  • Select an option

  • Save OhadRubin/dc94a0b6b7ff42d1b5e8d3cdd0a64284 to your computer and use it in GitHub Desktop.

Select an option

Save OhadRubin/dc94a0b6b7ff42d1b5e8d3cdd0a64284 to your computer and use it in GitHub Desktop.
HOWTO: Save a Running Process You Forgot to Start in tmux

HOWTO: Save a Running Process You Forgot to Start in tmux

You started a long-running job and forgot to use tmux. Now you're stuck. Here's how to save it.

Option 1: Detach with disown (simplest, lose output)

# In the terminal where the process is running:
Ctrl+Z              # Suspend the process
bg                  # Resume in background
disown              # Detach from shell

The process will survive terminal close, but output goes nowhere.

Option 2: Redirect output with gdb (keep logging)

First, install gdb if needed:

sudo apt-get install -y gdb

Find your process:

pgrep -af "your_script"

Redirect stdout/stderr to a file:

sudo gdb -p <PID> -batch \
  -ex 'call close(1)' \
  -ex 'call open("/tmp/output.log", 1089, 0644)' \
  -ex 'call close(2)' \
  -ex 'call open("/tmp/output.log", 1089, 0644)' \
  -ex 'detach'

The magic number 1089 = O_WRONLY|O_CREAT|O_APPEND (write, create, append).

Now you can:

tail -f /tmp/output.log

Option 3: reptyr into tmux (ideal, but often fails)

sudo apt-get install -y reptyr
tmux new -s rescued
reptyr <PID>

This often fails if the process has subprocesses. Use Option 2 instead.

Prevention

Next time:

tmux new -s training
./your_long_script.sh
# Ctrl+B, D to detach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment