c++ - 有没有办法恢复/恢复 nohup 以在控制台中查看输出?

标签 c++ c linux file-descriptor nohup

我知道机会极低,但有没有办法 查看 nohup-ed 进程最近输出了什么?

我仍然打开这个进程,但我运行它时将所有输出重定向到/dev/null。

那么有没有办法将这样的过程恢复到控制台,或者它是没有希望的:(

干杯

最佳答案

有一种方法,但不是直截了当的,诀窍是使用 dup2 并取决于您的程序链接到 libc 的事实(所有 c/c++ 应用程序都是,但 java 应用程序不会例如)

  1. 使用 gdb 附加到您的进程
  2. 在 gdb 提示符下运行以下命令将标准输出重定向到/tmp/newfile

    $ print dup2(open("/tmp/newfile",0), 1)

  3. 运行以下命令将 stderr 重定向到/tmp/newfile

    $ print dup2(open("/tmp/newfile",0), 2)

  4. 从您的进程中分离 gdb 并完成

什么 dup2确实是

duplicate a file descriptor This means that both stdout/stderr (1 and 2) and the new file descriptor returned from open can be used interchangeable which will cause all output going to stdout and stderr to go to the file you opened.

关于c++ - 有没有办法恢复/恢复 nohup 以在控制台中查看输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4265899/

相关文章:

c++ - rand()/srand() 不断给我零

android - NDK、LOCAL_EXPORT_C_INCLUDES 未按预期工作

c - 释放结构内部的指针(指向 void*)

无参数传递或返回值的 C 程序汇编函数

linux - 如何减少 Linux Ubuntu 终端中命令前的字数?

c++ - 将 GDAL 合并到 MSVS 2008 C++ 项目中

c++ - 对一个类中的一个 vector 进行排序,第二个 vector 应与第一个 vector 一起移动

c - argv 字符串上的 malloc 错误

linux - 来自不同 IP 的 Wordpress 不受控制的提要请求

python - json.dump 时如何显式设置回车?