Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to restore/recover nohup to see the output in the console?

I know chances are extremely low, but is there a way to see what a nohup-ed process has been outputting recently?

I still have this process open but I've run it with redirecting all output to /dev/null.

So is there a way to recover such process back to console, or is it hopeless :(

Cheers

like image 407
Itako Avatar asked Nov 24 '10 10:11

Itako


1 Answers

There is a way but it isn't straight forward, the trick is to use dup2 and depends on the fact that your program is linked against libc (which all c/c++ applications would be, but a java application wouldn't be for example)

  1. attach to your process using gdb
  2. run the following at the gdb prompt to redirect stdout to /tmp/newfile

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

  3. run the following to redirect stderr to /tmp/newfile

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

  4. detach gdb from your process and your done

What dup2 does is

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.

like image 184
hhafez Avatar answered Nov 10 '22 00:11

hhafez