在unix,maxosx或者linux下工作的时候,经常需要长时间运行一个程序,通常会把程序放到后台,然后通过日志来监控。
如果使用screen的话,可以让程序保持前台运行,又能通过detach放到后台。
如果在公司机器上用screen运行一个程序,然后detach,回家后通过ssh连接公司机器,用screen restore就可以继续观察程序运行了。
可是如果一开始没有用screen运行,直接放置到后台怎么办呢。reptyr就派上用场了,以下是reptyr的说明

reparent a orphan background process, means background a process in a terminal and bring it back to foreground to another 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.)

A few blog posts by its author:

    reptyr: Attach a running process to a new terminal
    reptyr: Changing a process’s controlling terminal