11

I don't well what's the name of the situation, I think yesterday I closed a session with a background ipython process, but I am still able to connect to it (the process is running a web server and I can load the page). Can I still get to see the stdout/stderr of the process?

If I run ps -f -Umyname I get (I deleted some unrelated lines, but the PID and PPID are the real ones):

UID        PID  PPID  C STIME TTY      STAT   TIME CMD
16131     1767  1765  0 11:20 ?        S      0:03 sshd: myname@notty
16131     1946 21019  0 11:25 ?        Sl     1:47 /usr/bin/python -m IPython.kernel -f /home/myname/.ipython/profile_default/security/kernel-c72091
16131     3946 21019  0 14:37 pts/3    Ss+    0:00 /bin/bash
16131     4003  4001  0 14:38 ?        S      0:01 sshd: myname@pts/14
16131     4004  4003  0 14:38 pts/14   Ss     0:00 -bash
16131     6366  4004  0 16:41 pts/14   R+     0:00 ps -f Umyname
16131    21019     1  0 Jul30 ?        Sl     0:52 /usr/bin/python ipython notebook 

I am sure it's not being ran inside a screen. Any idea? I am interest in PID 21019

1
  • Would like to discover best techical name for this situation, too :) May 5 at 10:40
10

You can use strace to see the output of a running process.

$ strace -p 21019 -e write=1,2

The above command will attach to the process with PID 21019 and trace write-calls to fd 1 (stdout) and 2 (stderr).

When you're done, you can send a sigint (to strace) to detach (press ctrl + c).

edit: There are also some tools that allow you to attach a running process to a new terminal, e.g. reptyr, screenify, retty. Note that most of these use strace, gdb or similar trace-tools to accomplish this, but they tend to produce prettier output.

4
  • I got attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted Could not attach to process. If your uid matches the uid of the target process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf Unfortunately I have not root access in this system, but thanks for the suggestion
    – laika
    Aug 5 '15 at 7:07
  • 2
    strace can not run in docker github.com/moby/moby/issues/20064, To turn off security features use: docker run --security-opt seccomp:unconfined ...
    – Mithril
    Jun 28 '17 at 7:23
  • I had the same sudo issue. Did it afterwards with sudo, and it's hanging on restart_syscall(<... resuming interrupted poll ...> Feb 13 '19 at 9:04
  • Is there an easy way to tell strace to only output ASCII line by line, as if I had started the process in a terminal? Right now I see both ASCII and hex but the ASCII is all garbled / split up over multiple lines.
    – balu
    Apr 12 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.