我可以 nohup/screen 一个已经启动的进程吗?

我可以 nohup/screen 一个已经启动的进程吗?

我正在通过 SSH 对一些需要长时间运行的数据迁移脚本进行测试运行。假设我在下午 4 点左右开始运行一个脚本;现在,下午 6 点就到了,我诅咒自己没有把这一切都做完screen

有没有办法“追溯”nohup一个进程,还是我需要整晚让电脑在线?如果无法附加screen到/nohup我已经启动的进程,那为什么?与父/子进程如何交互有关?(我不会接受“不”的答案,至少不能解决“为什么”的问题——抱歉;))

答案1

如果你使用 Bash,你可以运行disown -h job

否认

disown [-ar] [-h] [jobspec ...]

如果没有选项,则每个 jobspec 都会从活动作业表中删除。如果-h给出了选项,则不会从表中删除作业,但会对其进行标记,以便在 shell 收到 SIGHUP 时不会向该作业发送 SIGHUP。如果不存在 jobspec,并且未提供-anor-r选项,则使用当前作业。如果没有提供 jobspec,则该-a 选项表示删除或标记所有作业;-r没有 jobspec 参数的选项将操作限制为正在运行的作业。

答案2

使用雷普特

摘自自述文件:

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.)

其作者发表的一些博客文章:

答案3

要将进程从一个 tty 窃取到您当前的 tty,您可能需要尝试以下方法:

http://www.ucc.asn.au/~dagobah/things/grab.c

它需要重新格式化才能编译为当前的 Linux/glibc 版本,但仍然有效。

答案4

低温生物是 grab.c 作者的进一步开发,它将进程冻结到一个文件,然后运行该文件(在屏幕内)以恢复该进程。

相关内容