Can one pick up a running application from terminal?

16,437

Solution 1

Each process in linux has a special directory /proc/{pid}/fd/. 0 is stdin, 1 is stdout and 2 is stderr. So, assuming you are only interested in diagnostic output you can determine the process pid, and then in the terminal do:

to see stdout:

cat /proc/{pid of process}/fd/1

to see stderr:

cat /proc/{pid of process}/fd/2

Solution 2

Or you can use strace like this

sudo strace -p $pid_of_the_process

Solution 3

You can attach with gdb to a running process.

The syntax is

gdb program pid

Ok, you cannot see source code, if debug information are stripped, which is the default for deployed applications. But you can probably see stdout/stderr and debugger messages, segfaults.

Solution 4

You are likely looking for retty. You could also look for "attach tty". Basically it's done using ptrace so you could even roll your own.

Share:
16,437

Related videos on Youtube

neydroydrec
Author by

neydroydrec

South Asianist, polyglot, amateur programmer, urban farming entrepreneur, development analyst, philosopher, control freak, coffee addict, etc. etc.

Updated on September 18, 2022

Comments

  • neydroydrec
    neydroydrec over 1 year

    Sometime I need to run an application from terminal for debugging. If I am sure the bug will occur short after launching the application, I can run this application from the Terminal.

    However, bugs occur unexpectedly, and then only I need to monitor the buggy application from the Terminal (to see its output).

    Can I then pick up an application from terminal, which was not launched using Terminal? If so how?

    • neydroydrec
      neydroydrec over 12 years
      I eventually picked up Michał Šrajer's answer because it was the most instructive. But all your answers were just good and appreciated too. Cheers.
    • Lekensteyn
      Lekensteyn over 12 years
      Previous error messages (to stderr) are written to ~/.xsession-errors if you launched it via GUI.
  • neydroydrec
    neydroydrec over 12 years
    I tried that but it didn't work. For instance System Monitor shows Cairo-Dock ID is 1452, so I run gdb program 1452 (I also tried gdb 1452), but it returns "1452: No such file or directory. "
  • enzotib
    enzotib over 12 years
    The first one should work. What did it return?
  • neydroydrec
    neydroydrec over 12 years
    OK i'm a bit confused, when I run cat /proc/1840/fd/1 it seems to return output from other processes than the one I picked up. (1840 is currently for Skype according to System Monitor) It shows output of other programs too... :|
  • neydroydrec
    neydroydrec over 12 years
    This is what it returns fully after asking for pid 1840 (Skype): GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2 Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i686-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... 1840: No such file or directory.
  • enzotib
    enzotib over 12 years
    I said the first one, that with gdb program pid. I think the output you shown is from gdb pid
  • neydroydrec
    neydroydrec over 12 years
    Sorry, my mistake. `gdb program 3385 (again Skype) returns this.
  • enzotib
    enzotib over 12 years
    @Benjamin: I see Could not attach to process, it is a root owned process?
  • enzotib
    enzotib over 12 years
    @Benjamin: ps -ef | grep [p]rocessname should show the user in the first field.
  • neydroydrec
    neydroydrec over 12 years
    it's a user process.