• Stars
    star
    5,339
  • Rank 7,737 (Top 0.2 %)
  • Language
    C
  • License
    MIT License
  • Created almost 14 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reparent a running program to a new terminal

reptyr - A tool for "re-ptying" programs.

reptyr is a utility for taking an existing running program and attaching it to a new terminal. Started a long-running process over ssh, but have to leave and don't want to interrupt it? Just start a screen, use reptyr to grab it, and then kill the ssh session and head on home.

USAGE

reptyr PID

"reptyr PID" will grab the process with id PID and attach it to your current terminal.

After attaching, the process will take input from and write output to the new terminal, including ^C and ^Z. (Unfortunately, if you background it, you will still have to run "bg" or "fg" in the old terminal. This is likely impossible to fix in a reasonable way without patching your shell.)

Typical usage pattern

  • Start a long running process, e.g. top
  • Background the process with CTRL-Z
  • Resume the process in the background: bg
  • Display your running background jobs with jobs -l, this should look like this:
    • [1]+ 4711 Stopped (signal) top
    • (The -l in jobs -l makes sure you'll get the PID)
  • Disown the jobs from the current parent with disown top. After that, jobs will not show the job any more, but ps -a will.
  • Start your terminal multiplexer of choice, e.g. tmux
  • Reattach to the backgrounded process: reptyr 4711
  • Detach your terminal multiplexer (e.g. CTRL-A D) and close ssh
  • Reconnect ssh, attach to your multiplexer (e.g. tmux attach), rejoice!

"But wait, isn't this just screenify?"

There's a shell script called "screenify" that's been going around the internet for nigh on 10 years now that uses gdb to (supposedly) accomplish the same thing. The difference is that reptyr works much, much, better.

If you attach a "less" using screenify, it will still take input from the old terminal. If you attach an ncurses program using screenify, and resize the window, your program won't notice. If you attach a process with screenify, ^C in the new terminal won't work.

reptyr fixes all of these problems, and is the only such tool I know of that does so. See below for some more details on how it accomplishes this.

PORTABILITY

reptyr supports Linux and FreeBSD. Not all functionality is currently available on FreeBSD. (Notably, FreeBSD doesn't support reptyr -T at this time.

reptyr uses ptrace to attach to the target and control it at the syscall level, so it is highly dependent on details of the syscall API, available syscalls, and terminal ioctl()s. A port to other operating systems may be technically feasible, but requires significant low-level knowledge of the relevant platform, and may entail significant refactors.

reptyr works on i386, x86_64, and ARM. Ports to other architectures should be straightforward, and should in most cases be as simple as adding an arch/ARCH.h file and adding a clause to the ifdef ladder in ptrace.c.

ptrace_scope on Ubuntu Maverick and up

reptyr depends on the ptrace system call to attach to the remote program. On Ubuntu Maverick and higher, this ability is disabled by default for security reasons. You can enable it temporarily by doing

# echo 0 > /proc/sys/kernel/yama/ptrace_scope

as root, or permanently by editing the file /etc/sysctl.d/10-ptrace.conf, which also contains more information about exactly what this setting accomplishes.

reptyr -l

As a bonus feature, if you run "reptyr -l", reptyr will create a new pseudo-terminal pair with nothing attached to the slave end, and print its name out.

If you are debugging a program in gdb, you can pass that name to "set inferior-pty". Because there is no existing program listening to that tty, this will work much better than passing an existing shell's terminal.

How does it work?

The main thing that reptyr does that no one else does is that it actually changes the controlling terminal of the process you are attaching. I wrote a blog post explaining just what the shenanigans involved are.

PRONUNCIATION

I pronounce it like "repeater", but since that's easily ambiguous, "re-P-T-Y-er" is also acceptable.

CREDITS

reptyr was written by Nelson Elhage [email protected]. Contact him with any questions or bug reports.

URL

http://github.com/nelhage/reptyr

More Repositories

1

llama

Go
585
star
2

gojit

JIT code-generation in Go!
Go
338
star
3

rules_boost

bazel build rules to use boost in bazel projects
C++
287
star
4

ministrace

A minimal toy implementation of strace(1)
C
161
star
5

reverse-android

Reverse-engineering tools for Android applications
Emacs Lisp
55
star
6

taktician

An implementation of and AI for the game of Tak
Go
55
star
7

elisp

nelhage's emacs configuration
Emacs Lisp
47
star
8

bemu

A just-in-time compiler for MIT 6.004's "Beta" processor.
C++
36
star
9

virtunoid

My KVM breakout code from my DEFCON/Black Hat 2011 presentation
C
35
star
10

transformer-rs

A sketch of a Transformer in Rust for a blog post
Rust
28
star
11

ultimattt

A Rust Implementation of Ultimate Tic Tac Toe
Rust
23
star
12

crossme

A collaborative crossword-puzzle solver written on Meteor
JavaScript
23
star
13

util-scripts

~/bin/
Shell
14
star
14

pw

My personal GPG-based password manager
Go
13
star
15

nullderef

A module for playing with kernel NULL pointer dereferences
C
11
star
16

iron-blogger

Code to run the Iron Blogger event.
Python
9
star
17

re2

A git mirror of Russ Cox's re2 regular-expression library
C++
9
star
18

s3multiget

Some benchmarks of S3 multigets
Go
9
star
19

aoc2023

Advent of Code 2023
C++
9
star
20

x11-proxy

8
star
21

git-merge-rename

A Dropbox-inspired git merge strategy that never fails
Shell
8
star
22

jsbeta

An emulator for 6.004's Beta processor for the browser.
JavaScript
8
star
23

plv8js

Fork of http://code.google.com/p/plv8js
C++
8
star
24

github-downloader

Scripts to download all of github. Or at least much of it.
Python
7
star
25

librstpreload

A LD_PRELOAD library to make any app support Random Standard Time.
C
6
star
26

go.cli

Some utilities for writing CLIs in go.
Go
6
star
27

ghostscript-afl

fuzzing ghostscript with AFL
C
5
star
28

kernel-workshop

demo kernel modules for a workshop at recurse center
C
5
star
29

blog.nelhage.com

blog.nelhage.com
JavaScript
5
star
30

tf-experiments

some experiments with tensorflow and tf on ec2
Python
5
star
31

rules_fuzzer

bazel+libfuzzer
Python
4
star
32

barnowl-zstatus

Z-Status module for BarnOwl
Perl
4
star
33

check-plus

A simple EDSL for testing C code using Check
C
4
star
34

flnv

nelhage's experimental toy scheme implementation
C
3
star
35

chef-config-nelhage

chef config for my personal infrastructure
Python
3
star
36

barnowl-devutils

DevUtils module for BarnOwl
Perl
3
star
37

barnowl-alias

Alias module for BarnOwl
Perl
2
star
38

coop

A Coq playground
Coq
2
star
39

iron-gollum

Rust
2
star
40

authmitedu

Code for auth.mit.edu OpenID provider
Perl
2
star
41

gollum

A toy typed lambda-calculus
Go
2
star
42

mitua

2
star
43

barnowl-modules

Superrepo for my BarnOwl modules
2
star
44

data-sexpression

A perl S-Expression parser
Perl
2
star
45

snb

Sexy Nerd Bot - A bot for the MIT prefrosh chat room
2
star
46

accidentallyquadratic

I accidentally an n²
Python
2
star
47

barnowl-vt-asedeno

VT_ASedeno style for BarnOwl
Perl
2
star
48

bazel_git_repositories

Implementation of bazel `git_repository` remotes using Skylark
Python
2
star
49

hszephyr

Haskell libzephyr bindings
Haskell
2
star
50

claude

Scrappy CLI for Claude
Go
2
star
51

duckdb-parquet-bugs

Parquet files and generators for some DuckDB parquet bugs
Go
2
star
52

HIBP-reader

reader for HIPB sha files
Python
1
star
53

Ascension

Jifty webapp to track progress in nethack
Perl
1
star
54

arduinoproject

aggregator of random arduino projects
Assembly
1
star
55

tlaplus-sandbox

toying with TLA+
TLA
1
star
56

minimax.dev

Hugo site for https://minimax.dev/
HTML
1
star
57

brainfuck

Don't you have your own repository of brainfuck programs?
Brainfuck
1
star
58

magit

A magit fork with fixes/changes/hacks I've found useful.
Emacs Lisp
1
star
59

iobench

Some mostly-one-off IO benchmark scripts
C++
1
star
60

party-zephyr

A partychat<->Zephyr bridge
Python
1
star
61

proc-maps-bench

some benchmarking experiments related to /proc/$pid/maps
C++
1
star
62

godis86

golang wrapper for udis86
C
1
star
63

mode13h

Some code I wrote back in high school for doing graphics using VGA Mode 13h
C
1
star
64

mongod-tests

Framework for writing mongod tests and reproducers
Python
1
star
65

haskell-hunt

Various Haskell utilities to help with solving Mystery Hunt puzzles
Haskell
1
star
66

langer

C
1
star
67

xformer

Some toy transformer implementations
Python
1
star
68

rubik

Rubik's Cube implementation and experiments
C++
1
star
69

aoc2021

Advent of Code 2021
Julia
1
star
70

asciisnowmanforyou.com

http://asciisnowmanforyou.com
HTML
1
star
71

Text-Index-Database

An old attempt at writing my own full-text indexing solution
Perl
1
star