You started a long-running job and forgot to use tmux. Now you're stuck. Here's how to save it.
# In the terminal where the process is running:
Ctrl+Z # Suspend the process
bg # Resume in background
disown # Detach from shellThe process will survive terminal close, but output goes nowhere.
First, install gdb if needed:
sudo apt-get install -y gdbFind 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.logsudo apt-get install -y reptyr
tmux new -s rescued
reptyr <PID>This often fails if the process has subprocesses. Use Option 2 instead.
Next time:
tmux new -s training
./your_long_script.sh
# Ctrl+B, D to detach